OStack程序员社区-中国程序员成长平台

标题: iphone - AFNetworking 在 JSON 中编码 POST 参数? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:35
标题: iphone - AFNetworking 在 JSON 中编码 POST 参数?

我有以下代码:

APIClient *client = [APIClient sharedManager];
NSMutableURLRequest *request = [client requestWithMethod"OST"
                                                    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 编码?



Best Answer-推荐答案


您必须将客户端的 parameterEncoding 设置为 AFJSONParameterEncoding,因为默认值已经是 AFFormURLParameterEncoding

关于iphone - AFNetworking 在 JSON 中编码 POST 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16600769/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4