objective c - Translating cURL request to NSMutableURLRequest -
i'm new objective-c developer , i'm interacting api in curl format. i'm used making calls using urls, pieced request found on internets. i'm still not able pull data in app.
this original curl request (with dummy keys of course):
curl -v -h "app_id:12345" -h "app_key:abcdefg" -x post "http://data.host.com/object" -d '{"page":0,"take":10}'
this attempt:
//request nsurlsession *session = [nsurlsession sharedsession]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"http://data.host.com/object"]]; //set method request.httpmethod = @"post"; //set parameters nsdictionary *parameters = @{ @"page": @(0), @"take": @(10) }; nsmutablestring *parameterstring = [nsmutablestring string]; (nsstring *key in [parameters allkeys]) { if ([parameterstring length]) { [parameterstring appendstring:@"&"]; } [parameterstring appendformat:@"%@=%@", key, parameters[key]]; } nslog(@"parameter string: %@",parameterstring); //set headers [request setvalue:@"12345" forhttpheaderfield:@"app_id"]; [request setvalue:@"abcdefg" forhttpheaderfield:@"app_key"]; [request sethttpbody:[parameterstring datausingencoding:nsutf8stringencoding]]; nsurlsessiondatatask *task = [session datataskwithrequest:request completionhandler:^(nsdata *data, nsurlresponse *response, nserror *error) { if (!error) { if ([data length]) { nsdictionary *jsonresponse = [nsjsonserialization jsonobjectwithdata:data options:0 error:nil]; nslog(@"json response: %@", jsonresponse); } } else { nslog(@"%@", error); } }]; [task resume]; nslog(@"task: %@", task);
i don't error, jsonresponse returns null. have idea on i'm missing? in advance!
you see difference if compared http message exchanges between curl version , obj-c version. afaics you're missing header content type specify encoding of body. when posting need pass information on how encoding body. here example code 1 of apps:
- (nsurlrequest *)createpostrequestwithurl:(nsurl *)url parameters:(nsdictionary *)parameters { nslog(@"startgettaskforurl: %@, params %@", url, parameters); nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; [request sethttpmethod:@"post"]; [request addvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; nsstring * httpparams = [self createhttpparameters:parameters]; nslog(@"httpclient: postrequestwithurl body: %@", httpparams); [request sethttpbody:[httpparams datausingencoding:nsutf8stringencoding]]; return request; } - (nsstring *)urlencodedutf8string: (nsstring *) source { return (id)cfbridgingrelease(cfurlcreatestringbyaddingpercentescapes(0, (cfstringref)source, 0, (cfstringref)@";/?:@&=$+{}<>,", kcfstringencodingutf8)); } - (nsstring *) createhttpparameters: (nsdictionary *) parameters { nsmutablestring *body = [nsmutablestring string]; (nsstring *key in parameters) { nsstring *val = [parameters objectforkey:key]; if ([body length]) [body appendstring:@"&"]; [body appendformat:@"%@=%@", [self urlencodedutf8string: [key description]], [self urlencodedutf8string: [val description]]]; } return body; }
Comments
Post a Comment