AFNetworking 2.0 multipart request body blank

前端 未结 3 929
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 00:47

Similar to this issue.

Using AFNetworking 2.0.3 and trying to upload an image using AFHTTPSessionManager\'s POST + constructingBodyWithBlock. For reasons unknown, it se

3条回答
  •  孤城傲影
    2021-02-10 01:05

    Digging into this further, it appears that when you use NSURLSession in conjunction with setHTTPBodyStream, even if the request sets Content-Length (which AFURLRequestSerialization does in requestByFinalizingMultipartFormData), that header is not getting sent. You can confirm this by comparing the allHTTPHeaderFields of the task's originalRequest and currentRequest. I also confirmed this with Charles.

    What's interesting is that Transfer-Encoding is getting set as chunked (which is correct in general when the length is unknown).

    Bottom line, this seems to be a manifestation of AFNetworking's choice to use setHTTPBodyStream rather than setHTTPBody (which doesn't suffer from this behavior), which, when combined with NSURLSession results in this behavior of malformed requests.

    I think this is related to AFNetworking issue 1398.

提交回复
热议问题