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
            }
        ]
    }
}
2
  • Looks like it: stackoverflow.com/questions/299628/… Commented Aug 5, 2014 at 14:16
  • did u handle the DELETE request. i am stuck in same trouble Commented Mar 25, 2016 at 15:21

2 Answers 2

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];
Sign up to request clarification or add additional context in comments.

1 Comment

how is it possible to achieve above . i am stuck in same trouble
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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.