Reputation: 6353
I'm using JSONHTTPClient library from github in my Objective c application. I have a lot of calls to my api and all works, but when I try add a POST function with headers and body, I get an error:
Error Domain=JSONModelErrorDomain Code=2 "Bad network response. Probably the JSON URL is unreachable." UserInfo={NSLocalizedDescription=Bad network response. Probably the JSON URL is unreachable.}
I use the JSONFromURLWithString url to do a POST call. The api call works correctly, returning the data from server (I'm tested this from postman) But from this function in my objective c application I'm getting always error, why not get the response?
Am I sendind any data in bad format?
This is my code:
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"var1", @"keyVar1", @"5" , @"keyvar2", nil];//Here initialize my headers values into a dictionary
NSDictionary *dicty = [NSDictionary dictionaryWithObjectsAndKeys:@"6234", @"id", @"4324" , @"id2", nil];//This is my body
NSData *jsonDatass = [NSJSONSerialization dataWithJSONObject:dicty options:0 error:nil];//Here convert my body to nsdata
[JSONHTTPClient JSONFromURLWithString:@"myURL" method:@"POST" params:nil orBodyData:jsonData headers:parameters completion:^(id json, JSONModelError *err){
NSLog(@"have error?: %@", err);
NSLog(@"have response?: %@", json);
}];
Upvotes: 0
Views: 91
Reputation: 98
I think for pass json in url first you need to convert json in to the string and then pass then string as a argument. Thats why in postman it works well.
I want to know the complete URL after you joining together,in the
[JSONHTTPClient JSONFromURLWithString:@"myURL" method:@"POST" params:nil
orBodyData:jsonData headers:parameters completion:^(id json, JSONModelError *err){
}
method,your complete URL shoud be same as
******************************** divider ***********************************
NSString *myString = [[NSString alloc] initWithData:jsonDatass encoding:NSUTF8StringEncoding];
myString = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *uploadUrl = [NSString stringWithFormat:@"<YOU host URL>"?data=%@",myString];
also parameters
Should be converted to string to splice into the url.
Upvotes: 0