0

I am new in xcode and have an issue for sending request to server using AFNetworking.

Following is json request:

{
  "method": "loginmethod",
  "credentials": {
      "credentials": {
          "email": "email@email.com,
          "pwd": "1111111"
      }
  }
}

My code is following:

NSDictionary *parameters = 
      @{
        @"method": @"loginmethod",
        @"credentials":@{
             @"credentials": @{
                   @"email": @"email@email.com",
                   @"pwd": @"111111",
             }
         }
       };

AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"application/json", nil];
[sessionManager POST: @"http://xxxxx.com/service.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@" %@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"%@", error);
}];

Server doesn't accept this json request. So response is wrong account.

I think I have a mistake in making a json request. Or what I have mistake? I wish you advance me. Thanks.

Following is a screenshot of Postman.

screenshot of Postman

Water Flower
  • 377
  • 7
  • 23
  • What's the `responseObject` or `error` saying? – Larme Jan 05 '19 at 10:33
  • Thanks for your reply. I can get responseObject. But wrong response. That is server can receive request, but can't get "method" and "credentials". So I mean my code can connect to server but I have mistake to making json request. – Water Flower Jan 05 '19 at 13:44
  • "Following is json request:" Is that from the doc? Is it inside the JSON, or does it mean that the the path to append at the URL is "/loginmethod"? – Larme Jan 05 '19 at 14:56
  • I upload the screenshot using the Postman. I can get response from Postman. I need as like function. Pls see the screenshot. – Water Flower Jan 05 '19 at 15:04
  • You can use Postman to generate Objective-C Code, to see where goes the values and how (httpBody, headers, what's the format, etc.), and compare. – Larme Jan 05 '19 at 15:06
  • Yes. you are right. but in Postman there is raw data. I want to use AFNetworking and use json as a request. If u know that pls help me. – Water Flower Jan 05 '19 at 15:15
  • what do you mean by "wrong response"? What is the output of that `NSLog (@"%@", responseObject)` line? `responseObject` should be a dictionary containing the json of the response shown by Postman: you can just access it as it comes or deserialize it in a custom object. See https://stackoverflow.com/a/21962445/2661320 – il3v Jan 07 '19 at 10:23
  • can you try using raw data please and choose type json in postman – youssef ali Apr 07 '19 at 07:16

0 Answers0