Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
            }
        ]
    }
}
like image 762
Eyeball Avatar asked Aug 05 '14 14:08

Eyeball


2 Answers

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];
like image 147
f3n1kc Avatar answered Oct 31 '22 00:10

f3n1kc


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"]
like image 29
Norak Avatar answered Oct 31 '22 01:10

Norak