Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting JSON as the body of a POST request using AFHTTPClient

I am trying to find a way, using AFNetworking, to set the Content-Type header to be application/json and to POST with JSON in the body. The methods that I'm seeing in the documentation (postPath and requestWithMethod) both take a dictionary of parameters, which I assume is encoded in the standard form syntax. Does anyone know of a way to instruct AFHTTPClient to use JSON for the body, or do I need to write the request on my own?

like image 714
Eric Andres Avatar asked Oct 30 '11 03:10

Eric Andres


People also ask

Can we send JSON object in post request?

Specify the POST data: As per the HTTP specification for a POST request, we pass data through the message body. Using requests, you'll pass the payload to the corresponding function's data parameter. Data can be anything including JSON, dictionary, a list of tuples, bytes, or a file-like object.

How do I send a JSON POST request?

To post JSON data to the server, we need to use the HTTP POST request method and set the correct MIME type for the body. The correct MIME type for JSON is application/json. In this POST JSON example, the Content-Type: application/json request header specifies the media type for the resource in the body.

Can we send JSON via HTTP GET request?

To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.


2 Answers

I went ahead and checked out the latest AFNetworking from their master branch. Out of the box I was able to get the desired behavior. I looked and it seems like a recent change (October 6th) so you might just need to pull the latest.

I wrote the following code to make a request:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8080/"]];
[client postPath:@"hello123" parameters:[NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil] 
         success:^(id object) {
             NSLog(@"%@", object);
         } failure:^(NSHTTPURLResponse *response, NSError *error) {
             NSLog(@"%@", error);
         }];
[client release];

Under my proxy I can see the raw request:

POST /hello123 HTTP/1.1
Host: localhost:8080
Accept-Language: en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8
User-Agent: info.evanlong.apps.TestSample/1.0 (unknown, iPhone OS 4.3.2, iPhone Simulator, Scale/1.000000)
Accept-Encoding: gzip
Content-Type: application/json; charset=utf-8
Accept: */*
Content-Length: 21
Connection: keep-alive

{"k2":"v2","k1":"v1"}

From the AFHTTPClient source you can see that JSON encoding is the default based on line 170, and line 268.

like image 168
Evan Avatar answered Oct 22 '22 17:10

Evan


For me, json was NOT the default encoding. You can manually set it as the default encoding like this:

(using Evan's code)

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8080/"]];

[client setParameterEncoding:AFJSONParameterEncoding];

[client postPath:@"hello123" parameters:[NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil]
         success:^(id object) {
             NSLog(@"%@", object);
         } failure:^(NSHTTPURLResponse *response, NSError *error) {
             NSLog(@"%@", error);
         }];
[client release];

the crucial part:

[client setParameterEncoding:AFJSONParameterEncoding];
like image 41
stackOverFlew Avatar answered Oct 22 '22 16:10

stackOverFlew