我正在尝试从我的iOS应用程序向 RESTful Django Web服务发送 HTTP DELETE 请求。 我使用AFNetworking 2.0(2.4)。
在我的API调用的成功块中分析AFHTTPREquestOperation
之后,我发现请求的主体是零。参数URL在URL中编码和发送。
<AFHTTPRequestOperation: 0x10c587940,
state: isFinished,
cancelled: NO
request: <NSMutableURLRequest: 0x10c521ab0> {
URL: https://anURL.com/connections?data%5Bconnections%5D%5B%5D%5Bid%5D=106 },
response: <NSHTTPURLResponse: 0x10c5c7590> {
URL: anURL.com/
connections?data%5Bconnections%5D%5B%5D%5Bid%5D=106
}
{ status code: 200, headers {
Connection = "Keep-Alive";
"Content-Type" = "application/json";
Date = "Tue, 05 Aug 2014 14:07:53 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.7 (Ubuntu)";
"Transfer-Encoding" = Identity;
} }>
现在我想知道是否可以在请求正文中发送参数,就像使用 HTTP POST 而不是在URL中发送它们一样。这可能吗?
如何使用AFNetworking进行操作?
我如何发送atm:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager DELETE:host_url parameters:params success:success failure:failure];
我想发送的身体(它在上面的“params”参数中是什么):
{
"data": {
"connections": [
{
"id": 92
},
{
"id": 91
}
]
}
}
答案 0 :(得分:59)
有同样的问题。由于问题仍然存在,将在此处发布答案。您需要做的就是更改HTTP方法,将参数编码为查询字符串(默认为GET,HEAD和DELETE)。请求序列化程序具有HTTPMethodsEncodingParametersInURI
属性。只需将其设置为GET,HEAD即可完成:
serializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", nil];
答案 1 :(得分:2)
@ f3n1kc提供的答案已更正,但以防您查找Swift版本:
let manager = AFHTTPSessionManager()
manager.requestSerializer = AFHTTPRequestSerializer()
manager.requestSerializer.HTTPMethodsEncodingParametersInURI = ["GET", "HEAD"]