ios - cURL request in Objective-C (POST) -
i'm struggling translating these curl requests objective - c (i'll change api keys later):
get request:
curl -v -h "app_id:4bf7860a" -h "app_key:0026e51c7e5074bfe0a0c2d4985804b2" -x "http://data.leafly.com/strains/blue-dream" post request:
curl -v -h "app_id:4bf7860a" -h "app_key:0026e51c7e5074bfe0a0c2d4985804b2" -x post "http://data.leafly.com/strains" -d '{"page":0,"take":10}' i've been able 1 successful request far:
nsurl *url = [nsurl urlwithstring: [nsstring stringwithformat:@"http://data.leafly.com/strains/blue-dream"]]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; [request sethttpmethod:@"get"]; [request setvalue:@"application/json" forhttpheaderfield: @"content-type"]; [request addvalue:@"4bf7860a" forhttpheaderfield: @"app_id"]; [request addvalue:@"03d3eaa965c5809c5ac06a25505a8fe4" forhttpheaderfield:@"app_key"]; nsurlsessionconfiguration *defaultconfigobject = [nsurlsessionconfiguration defaultsessionconfiguration]; nsurlsession *session = [nsurlsession sessionwithconfiguration: defaultconfigobject delegate: nil delegatequeue: [nsoperationqueue mainqueue]]; nsurlsessiondatatask *task = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { nslog(@"data: %@",data); if (error) { nslog(@"error: %@", error); } else { nsdictionary *jsonresult = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:&error]; nslog(@"strain %@",jsonresult); } }]; [task resume]; i'm not able piece comprehensive way piece these request consistently (i've tried http://unirest.io/objective-c.html). can point me resource or me think through i'm doing wrong?
check code snippet out below should help.
nsstring *post = [[nsstring alloc] initwithformat:@"{page:0, take:10}"]; nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes]; nsurl *url = [nsurl urlwithstring:@"http://data.leafly.com/strains"]; nsmutableurlrequest *req = [nsmutableurlrequest requestwithurl:url]; [req sethttpmethod:@"post"]; [req addvalue:@"a2eaffe2" forhttpheaderfield: @"app_id"]; [req addvalue:@"49588984075af3d275a56c93b63eedc0" forhttpheaderfield:@"app_key"]; [req sethttpbody:postdata]; nsdata *res = [nsurlconnection sendsynchronousrequest:req returningresponse:null error:null]; nsstring *mystring = [[nsstring alloc] initwithdata:res encoding:nsutf8stringencoding]; nslog(@"%@", mystring);
Comments
Post a Comment