iphone - AFNetworking 在 JSON 中编码 POST 参数?
<p><p>我有以下代码:</p>
<pre><code>APIClient *client = ;
NSMutableURLRequest *request = [client requestWithMethod:@"POST"
path:
parameters:];
AFHTTPRequestOperation *operation = [ initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (callBack) {
callBack();
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failureBlock) {
failureBlock();
}
}];
;
</code></pre>
<p>这是这个请求在原始数据形式中的样子:</p>
<pre><code> $ 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"}
</code></pre>
<p>这就是 curl 的样子:</p>
<pre><code>comments$ curl --data "text=hello&look_id=1" -u foo:bar localhost:8888/look/1/comment
</code></pre>
<p>回复:</p>
<pre><code>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
</code></pre>
<p><strong>为什么对 POST 数据参数进行 JSON 编码?</strong></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您必须将客户端的 <code>parameterEncoding</code> 设置为 <code>AFJSONParameterEncoding</code>,因为默认值已经是 <code>AFFormURLParameterEncoding</code>。</p></p>
<p style="font-size: 20px;">关于iphone - AFNetworking 在 JSON 中编码 POST 参数?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16600769/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16600769/
</a>
</p>
页:
[1]