xjq233p_1
xjq233p_1

Reputation: 8060

AFNetworking encodes POST parameter in JSON?

I have the following code:

APIClient *client = [APIClient sharedManager];
NSMutableURLRequest *request = [client requestWithMethod:@"POST"
                                                    path:[NSString stringWithFormat:@"/look/%@/comment", cid]
                                              parameters:[NSDictionary dictionaryWithObjectsAndKeys:text, @"text", nil]];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    if (callBack) {
        callBack();
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (failureBlock) {
        failureBlock();
    }
}];

[operation start];

This is what this request looks like in raw data form:

[venv] $ nc -l 8888
POST /look/21624/comment HTTP/1.1
Host: localhost:8888
Authorization: Basic NjgwNDI6NDBiYzY2ZTBmODc5NTQ3MjczZmQxM2ZjMDJjNTYyZDIyNWU3YmRkOA==
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
Accept-Language: en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5
Accept: application/json
Content-Length: 25
Connection: keep-alive
User-Agent: Giordano.iPhone/1.0 (iPhone Simulator; iOS 6.1; Scale/1.00)

{"text":"testing text ignore"}

This is what it should look like with curl:

comments$ curl --data "text=hello&look_id=1" -u foo:bar localhost:8888/look/1/comment

Response:

POST /look/1/comment HTTP/1.1
Authorization: Basic NjgwNDI6NDBiYzY2ZTBmODc5NTQ3MjczZmQxM2ZjMDJjNTYyZDIyNWU3YmRkOA==
User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
Host: localhost:8888
Accept: */*
Content-Length: 20
Content-Type: application/x-www-form-urlencoded

text=hello&look_id=1

Why is the POST data arguments JSON encoded?

Upvotes: 2

Views: 4939

Answers (1)

Marcelo
Marcelo

Reputation: 9944

You must be setting your client's parameterEncoding to AFJSONParameterEncoding as the default already is AFFormURLParameterEncoding.

Upvotes: 4

Related Questions