I am trying to do a post via http with no succes here is my code
NSError *error;
NSHTTPURLResponse *response;
NSData *posting =
[NSJSONSerialization dataWithJSONObject:[input createJsonDictionary]
options:0
error:&error];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", kURL]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json"
forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue:[NSString stringWithFormat:@"AuthToken %@",token]
forHTTPHeaderField:@"Authorization"];
[urlRequest setHTTPBody:posting];
NSData *urlResponse =
[NSData dataWithData:[NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response error:&error]];
NSDictionary *jsonDictionary =
[NSJSONSerialization JSONObjectWithData:urlResponse
options:0
error:&error];
NSLog(@"status code: %i", [response statusCode]);
NSLog(@"URL: %@", [urlRequest URL]);
NSLog(@"header: %@ \n method: %@", [urlRequest allHTTPHeaderFields], [urlRequest HTTPMethod]);
NSLog(@"body: %@\n", [[NSString alloc] initWithData:[urlRequest HTTPBody] encoding:NSUTF8StringEncoding]);
In the NSLog I am getting back status code 500, there is a error. But when i try to do the same command using cURL
curl --request POST -H "Content-Type: application/json"
-H "Authorization: AuthToken TOKEN" --data '{"DATA"}' -sL
-w "\\n%{http_code} %{url_effective}\\n" https://URL.com
I get back status code 201, succes. Something seems to be off in my code. Also in the NSLogs for urlRequest they equal whats in the curl command. Is there something I’m doing incorrectly?
Found my own answer. The problem was that my code was making call to the server synchronously and the servers weren’t set up to do synchronous web calls. Instead I had to use a instant variable of NSURLConnection and do the call asynchronously “sendAsynchronousRequest:queue:completionHandler:” and use the methods start/cancel with the instant variable.