简体   繁体   中英

AFNetworking HTTP delete use body instead of URL

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).

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;
} }>

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?

How to do it using AFNetworking?

How i send atm:

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

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

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

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

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];

Answer provided by @f3n1kc is corrected, but just in case you looking for Swift Version:

let manager = AFHTTPSessionManager()
manager.requestSerializer = AFHTTPRequestSerializer()
manager.requestSerializer.HTTPMethodsEncodingParametersInURI = ["GET", "HEAD"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM