简体   繁体   中英

How to post json data to web server

I am new in XCode. I would like to send some data to web server which can only accept and send json data. So I tried the following code.

 NSMutableDictionary *newDatasetInfo = @{
                                  @"userName":@"abc",
                                  @"passWord":@"1234",
                                  @"apiKey":@"456789"
                                  }.mutableCopy;     
     NSError *myError = nil;
    NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:NSJSONWritingPrettyPrinted  error:&myError];


NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.HTTPAdditionalHeaders=@{
                                      @"Accept":@"application/json"
                                      };
NSURLSession *session=[NSURLSession sessionWithConfiguration:sessionConfig];   
NSString *postdatalength=[NSString stringWithFormat:@"%lu",(unsigned long)[post length]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc] init];
[request setURL:([NSURL URLWithString:@"http://xx.xx.xx.xx/efg/ijk/login"])];
[request setHTTPMethod:@"POST"];
[request setValue:postdatalength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:post];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"requestReply: %@", requestReply);             

}];
[postDataTask resume];

But after running the above code I got the below message

POSTed JSON could not be parsed.

Does anyone know whats the error here? Please help.

Replace below lines

NSMutableDictionary *newDatasetInfo = @{
                              @"userName":@"abc",
                              @"passWord":@"1234",
                              @"apiKey":@"456789"
                              }.mutableCopy;     
 NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:NSJSONWritingPrettyPrinted  error:&myError];

with

NSDictionary *newDatasetInfo = @{
                                        @"userName":@"abc",
                                        @"passWord":@"1234",
                                        @"apiKey":@"456789"
                                        };

NSError *myError = nil;
NSData *post = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:0  error:&myError];

And make sure your all parameters (like username,password etc) are true check it twice. you should check the service first in postman or advance rest client . these are the different tools to check web services.

Hope this will help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM