I have stuck here. Below is my code for Post request with raw body using NSURLSession. I got response = NULL and no error.
NSString* stringRequest = @"https://chocudan.com/api/shops/by_ids";
NSURL* urlRequest = [NSURL URLWithString:stringRequest];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:urlRequest];
request.HTTPMethod = @"POST";
NSString* bodyRequest = @"563c268b84ba489c4729f149";
//I have to tried a base64 convert here but still not work.
//request.HTTPBody = [NSData base64DataFromString:bodyRequest];
request.HTTPBody = [bodyRequest dataUsingEncoding:NSUTF8StringEncoding];
NSURLSessionConfiguration* configureSession = [NSURLSessionConfiguration defaultSessionConfiguration];
configureSession.HTTPAdditionalHeaders = @{@"Content-Type" : @"application/json charset=utf-8",
@"Content-Lenght" : @"180"};
NSURLSession* session = [NSURLSession sessionWithConfiguration:configureSession];
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse* respHttp = (NSHTTPURLResponse*) response;
if (!error && respHttp.statusCode == 200) {
NSDictionary* respondData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", respondData);
}else{
NSLog(@"%@", error);
}
}];
[dataTask resume];
I have to try with postman and everything work fine. This is pictures.
Thank in advance.
My raw data was like :
and what I did is :
This fixed it for me:
Try changing it too
I guess that it was not data that was null but
respondData
was null?That is because your service sends an Array with exactly one Object. JSONSerialisation creates an
NSArray
from that with oneNSDictionary
in it. The dictionary has the keys_id
,contact
and so on.So it is
BTW
does not make much of a sense but does not harm either.
With respect to the body of your request, see mihir's answer. He is just right. It may help you understanding mihir's point when you do this:
However, this is rather quick & dirty but may help you understanding the principles. Once understood you will certainly follow mihir's suggestion.
If you want to post raw, and param is a format of NSString, you only need to do this:
If we can’t get anything from that response, notice that the response serializer is correct. Any additional settings, please deal it with server.