-1

I will have to make the HTTP post call in iOS sdk,

curl https://www.googleapis.com/urlshortener/v1/url \ -H 'Content-Type: application/json' \ -d '{"longUrl": "http://www.google.com/"}'

Any help on this?

1
  • There are probably at least 2309823094832 answers to this question on this site. Did you look at any of them?
    – rooftop
    Aug 2, 2012 at 2:49

1 Answer 1

0

Plenty of answers to this available here and via google. However here's a code sample. If you need more info please do a deeper search.

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:POST_TARGET_URL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0];
[theRequest setHTTPMethod:@"POST"]; // set the method to post
[theRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",QUERY_VALUE_BOUNDRY] forHTTPHeaderField: @"Content-Type"];

NSMutableData *queryData = [[NSMutableData alloc] init];
for (NSString *key in [queryDict allKeys]) { // iterate the keys in an NSDictionary to build the post data with key/value pairs
    [queryData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\n\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
    [queryData appendData:[[NSString stringWithFormat:@"%@", [queryDict objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
    [queryData appendData:[[NSString stringWithFormat:@"\n--%@\n", QUERY_VALUE_BOUNDRY] dataUsingEncoding:NSUTF8StringEncoding]]; // begin bounds
}

[theRequest setHTTPBody:queryData]; // set the data object to the body of the post request

_URLConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; // open an NSURLConnection with the request that was just constructed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.