AFNetworking 500 response body

前端 未结 3 1955
野的像风
野的像风 2021-01-02 13:17

I\'ve been using AFNetworking 2.0 in my app. I\'ve noticed that if my web-service returns a 500 status code I do not get the body of the response.

Here is an example

3条回答
  •  执念已碎
    2021-01-02 14:01

    If you include my category in your project, it's as simple as the following:

    [mySessionManager POST:@"some-api" parameters:params success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
        ...
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        id responseObject = error.userInfo[kErrorResponseObjectKey];
        ... do something with the response ...
    }];
    

    Here's the code for my category. It swizzles AFURLSessionManager to inject a shim into the completion handler. The shim puts the response into the NSError's userInfo.

    https://gist.github.com/chrishulbert/35ecbec4b37d36b0d608

提交回复
热议问题