ios 错误域=com.alamofire.error.serialization.response 代码=-1011“请求失败:错误请求(400)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26235270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:03:48  来源:igfitidea点击:

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

iosobjective-ciphonejsonafnetworking

提问by user1954352

I am using AFnetworking library to post data on server.

我正在使用 AFnetworking 库在服务器上发布数据。

Following is my code to post data on server.

以下是我在服务器上发布数据的代码。

- (void) callLoginAPI:(NSDictionary *)dictProfile{
    // 1
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[dictProfile valueForKey:@"name"], @"username",
                                                                    [dictProfile valueForKey:@"first_name"],@"first_name",
                                                                    [dictProfile valueForKey:@"last_name"],@"last_name",
                                                                    [dictProfile valueForKey:@"email"],@"email",
                                                                    [dictProfile valueForKey:@"birthday"],@"dob",
                                                                    [dictProfile valueForKey:@"gender"],@"gender",
                                                                    [[dictProfile valueForKey:@"location"] valueForKey:@"name"],@"location",
                                                                    [dictProfile valueForKey:@"timezone"],@"timezone",
                                                                    @"",@"language",
                                                                    [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture?type=large",[dictProfile valueForKey:@"id"]],@"profile_pic_url",
                                                                    @"",@"cover_pic_url",nil];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:@"http://10.1.81.35:8000/api/login/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

and in response I got following error

作为回应,我收到了以下错误

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7c87b6f0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7cc220e0> { URL: http://10.1.81.35:8000/api/login/ } { status code: 400, headers {
    Allow = "POST, OPTIONS";
    "Content-Type" = "application/json";
    Date = "Tue, 07 Oct 2014 10:45:08 GMT";
    Server = "WSGIServer/0.1 Python/2.7.6";
    Vary = "Accept, Cookie";
    "X-Frame-Options" = SAMEORIGIN;
} }, NSErrorFailingURLKey=http://10.1.81.35:8000/api/login/, NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<7b226465 7461696c 223a2022 4a534f4e 20706172 73652065 72726f72 202d204e 6f204a53 4f4e206f 626a6563 7420636f 756c6420 62652064 65636f64 6564227d>}

I am not able to understand why I got the such kind of error. What is missing in my code?

我不明白为什么会出现这种错误。我的代码中缺少什么?

回答by mattt

The error says it all: you got a 400 responsefrom the server, meaning that what you sent was either not formatted properly, or the server just couldn't understand it.

错误说明了一切:您收到了来自服务器的400 响应,这意味着您发送的内容要么格式不正确,要么服务器无法理解。

回答by saraman

Please add this line to your code, i hope it help you.

请将此行添加到您的代码中,希望对您有所帮助。

manager.requestSerializer = [AFHTTPRequestSerializer serializer];

回答by Lal Krishna

I had experienced the same issue. In my case its only for upload Image with larger size. Our server has limited with max.upload size. I resized the image and uploaded, and the issue is gone.

我遇到过同样的问题。在我的情况下,它仅用于上传更大尺寸的图像。我们的服务器限制了 max.upload 大小。我调整了图像大小并上传,问题消失了。

You can resize the image with:

您可以使用以下方法调整图像大小:

+ (UIImage*)imageWithImage:(UIImage*)image
              scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}