AFNetworking HTTP delete use body instead of URL
Asked Answered
B

2

24

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
            }
        ]
    }
}
Babs answered 5/8, 2014 at 14:9 Comment(2)
Looks like it: #300128Gestapo
did u handle the DELETE request. i am stuck in same troubleNovercal
D
62

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];
Diadromous answered 10/3, 2015 at 11:51 Comment(2)
how is it possible to achieve above . i am stuck in same troubleNovercal
saved me, Thank you so muchVistula
D
2

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"]
Dearing answered 1/9, 2017 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.