AFNetworking HTTP删除使用正文而不是URL [英] AFNetworking HTTP delete use body instead of URL

查看:230
本文介绍了AFNetworking HTTP删除使用正文而不是URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我的iOS应用程序向 RESTful Django Web服务发送 HTTP DELETE 请求。
我使用AFNetworking 2.0(2.4)。

I'm trying to send a HTTP DELETE request to a RESTful Django web service from my iOS app. I use AFNetworking 2.0 (2.4).

在我的API调用的成功块中分析了 AFHTTPREquestOperation 之后,我发现请求的主体是零。 URL中编码并发送的参数URL。

After analysing the AFHTTPREquestOperation in the success block of my API call, i found that the body of the request is nil. The parameters URL encoded and sent in the 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 ,而不是在网址中发送。这是可能吗?

Now I wonder if it is possible to send the parameters in the body of the request, as done with HTTP POST instead of sending them in the URL. Is that possible?

如何使用AFNetworking?

How to do it using AFNetworking?

如何发送atm: / strong>

How i send atm:

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

[manager DELETE:host_url parameters:params success:success failure:failure];

我想要发送的主体(上面的params参数中的内容):

The body i want to send(its whats in the "params" parameter above):

{
    "data": {
        "connections": [
            {
                "id": 92
            },
            {
                "id": 91
            }
        ]
    }
}


推荐答案

有同样的问题。由于问题仍然存在,将在这里发布答案。所有你需要做的,是更改HTTP方法,将参数编码为查询字符串(默认GET,HEAD和DELETE)。请求序列化程序具有 HTTPMethodsEncodingParametersInURI 属性。只要将其设置为GET,HEAD即可:

Had same problem. As question is still open, will post answer here. All you need to do, is change HTTP methods, that encode parameters as a query string (by default GET, HEAD, and DELETE). Request serializer has HTTPMethodsEncodingParametersInURI property for that. Just set it to GET, HEAD and you are done:

serializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", nil];

这篇关于AFNetworking HTTP删除使用正文而不是URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆