gpt4 book ai didi

iphone - AFNetworking 在 JSON 中编码 POST 参数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:46:44 26 4
gpt4 key购买 nike

我有以下代码:

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 编码的?

最佳答案

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com