iOS应用程序中的AFNetworking使请求无法正常工作

时间:2013-07-12 18:56:05

标签: ios afnetworking put

我正在尝试发出一个put请求,以便能够更改我传入的参数,并且我收到400错误。

Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), 
got 400" UserInfo=0x8a7d7d0 {NSLocalizedRecoverySuggestion={"messages":{"error":
[{"code":400,"message":"Server can not understand Content-Type HTTP header media type
\"application\/x-www-form-urlencoded\""}]}},
AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest 
http://test.example.com/api/rest/customers/841>, 
NSErrorFailingURLKey=http://test.example.com/api/rest/customers/841, 
NSLocalizedDescription=Expected status code in (200-299), got 400, 
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a7e1e0>}

这是我的请求代码。

 ExampleHTTPClient *sharedClient = [ExampleHTTPClient sharedClient];
    NSDictionary *parameter = @{@"email":@"example@gmail.com",@"firstname":@"Bob", @"lastname":@"Jones"};

    [sharedClient setDefaultHeader:@"Content-Type" value:@"application/json"];

    [sharedClient putPath:@"/api/rest/customers/841" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response: %@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];

我刚开始使用网络服务,所以我的问题是了解正在发生的事情。

感谢任何帮助或想法,谢谢。

1 个答案:

答案 0 :(得分:0)

虽然我在设置AFNetworking客户端时无法解释您的问题但我使用了以下内容:

[self registerHTTPOperationClass: [AFJSONRequestOperation class]];
[self setDefaultHeader: @"Accept"
                 value: @"application/json"];

其中selfAFHTTPClient的子类的实例。

400的HTTP状态代码是:'错误请求 - 由于语法错误,服务器无法理解该请求。客户不应该在没有修改的情况下重复请求。您是否可以访问服务器日志以查看请求的结构?