Asha Sharma Asha Sharma - 2 years ago 712
iOS Question

Request failed: bad request (400) in "AFMultipartFormData"

I am working with afnetworking with objective c.
My parameter type is "formData". So I am using "AFMultipartFormData"

Here is my code

NSLog(@"enter in requestPostFormData");
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"application/json"];

[manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[manager.requestSerializer setValue:strAuthorization forHTTPHeaderField:@"Authorization"];

return [manager POST:baseURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData){

//passing parameter
NSMutableData *name = [[NSMutableData alloc]init];
[name appendData:[[NSString stringWithFormat:@"asha"] dataUsingEncoding:NSUTF8StringEncoding]];
formData appendPartWithFormData:name name:@"name"];

}success:^(NSURLSessionDataTask *task, id responseObject) {

NSLog(@"Response: %@", responseObject);

}failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error);


Now I am getting this error

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7feb71498f80> { URL: } { status code: 400, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 113;
"Content-Type" = "application/json; charset=utf-8";
Date = "Fri, 20 May 2016 10:49:01 GMT";
Server = nginx;
"Strict-Transport-Security" = "max-age=31536000";
Vary = "Cookie, Accept-Encoding";
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-XSS-Protection" = "1; mode=block";
} }, NSErrorFailingURLKey=,<7b0a2020 20202263 75727265 6e637922 3a205b0a 20202020 20202020 22546869 73206669 656c6420 69732072 65717569 7265642e 220a2020 20205d2c 200a2020 20202274 72616e73 61637469 6f6e5f69 64223a20 5b0a2020 20202020 20202254 68697320 6669656c 64206973 20726571 75697265 642e220a 20202020 5d2c200a 20202020 22656d61 696c223a 205b0a20 20202020 20202022 54686973 20666965 6c642069 73207265 71756972 65642e22 0a202020 205d2c20 0a202020 2022616d 6f756e74 223a205b 0a202020 20202020 20225468 69732066 69656c64 20697320 72657175 69726564 2e220a20 2020205d 2c200a20 20202022 70686f6e 65223a20 5b0a2020 20202020 20202254 68697320 6669656c 64206973 20726571 75697265 642e220a 20202020 5d0a7d0a>, NSLocalizedDescription=Request failed: bad request (400)}

As per my knowledge this error is getting when my URL is not generating proper. I think I am passing wrong way to parameter.

Please help how to resolve this issue.

Answer Source

400 Bad request means, you didn't form the request the way server expects.

Check the server logs for whats the issue..

and set content type text/html in your webservice.


I find somewhere following also help you

In the class AFURLRequestSerialization there is a property called HTTPMethodsEncodingParametersInURI and that is an NSSet that contains the http methods that are allowed to use params in the uri GET, HEAD, and DELETE by default.

You could override that and include POST as well.

in AFURLRequestSerialization.m lines 462 has an if statement that checks self.HTTPMethodsEncodingParametersInURI property contains POST. if it doesn't (as it doesn't by default), it will put the parameters in the body.

You can comment out that id statement for a quick test.

To get it to work I recommend overwriting the HTTPMethodsEncodingParametersInURI property.

when setting up your AFHTTPSessionManager it should look like this...

  AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:self.httpBaseUrl]];
   manager.requestSerializer = [AFHTTPRequestSerializer serializer];

   manager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithArray:@[@"POST", @"GET", @"HEAD", whatever other http methods you need here....]];

this should allow for sending a parameters in the uri of a POST.

UPDATE Try this:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithFormData:[key1 dataUsingEncoding:NSUTF8StringEncoding]

    [formData appendPartWithFormData:[key2 dataUsingEncoding:NSUTF8StringEncoding]

    // etc. for all

} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download