1

I am using AFNetworking to call a post web service, but every time i get the response

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)"

Here is my code

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [self.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json"]; [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; [parameters setObject:self.APIClientID forKey:@"client_id"]; [parameters setObject:self.APIClientSecret forKey:@"client_secret"]; [parameters setObject:self.APIGrantType forKey:@"grant_type"]; [manager POST:self.requestURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(task, responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(task, error); }]; 

It works in postman. Here is the request on postman Also, I found lot of answers for similar error, but it solved nothing.

8
  • Did you checked your API in postman first? Did you added App Transport Security Settings in your plist? Commented Sep 4, 2016 at 9:49
  • I did, and it works in postman. Commented Sep 4, 2016 at 9:51
  • Most probably your request isn't actually exactly like the one you send via Postman. Set the URL to some HTTP url and check the contents of your request with Wireshark or the like, and compare to the same thing sent with Postman. Commented Sep 4, 2016 at 12:02
  • Uh, you're sending JSON data with a Content-Type set to application/x-www-form-urlencoded... That's definitely a sign there's something wrong with your request. Commented Sep 4, 2016 at 12:10
  • I checked the request on postman and and the request on my app and they are the same. also, the Content-Type is the same as I send it over postman. Commented Sep 4, 2016 at 12:19

1 Answer 1

3

You are using a AFJSONRequestSerializer to serialise your request. This will create a JSON payload and not a url encoded form request.

Try using AFHTTPRequestSerializer instead.

Refer to the documentation here for more information.

You can also remove the following line of code as well, the correct request serializer will handle that for you:

[manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.