2

我有以下代码:

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];

这是这个请求在原始数据形式中的样子:

[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"}

这就是 curl 的样子:

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

回复:

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

为什么 POST 数据参数是 JSON 编码的?

4

1 回答 1

4

您必须将客户端设置parameterEncodingAFJSONParameterEncoding默认值已经是AFFormURLParameterEncoding.

于 2013-05-17T03:26:47.903 回答