How to convert curl post data to Objective C

89 Views Asked by At

Hi i want to send a post request to a server with api_key and version In curl i am able to send via the command

curl -X POST -F "[email protected]" "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=fc1isndcb9fb837d139b11644ksks8cf3d66e175&version=2016-05-20"

When i try to do it in Objective C using the code

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:URL]];

[request addValue:@"fc1isndcb9fb837d139b11644ksks8cf3d66e175" forHTTPHeaderField:@"api_key"];
[request addValue:@"2016-05-20" forHTTPHeaderField:@"version"];
[request setHTTPBody:strData];

NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

It shows api_key is invalid. Am i doing anything wrong?

1

There are 1 best solutions below

0
Itachi On

In your curl example, the api_key and version are put in the url path. In your objc example code, you put them in the header field, they are not the same.

To achieve the same result, you should merge the two parameters into the NSURL entity.