0
我試圖端口一個捲曲的請求:curl命令翻譯成NSMutableUrlRequest
curl -X POST -H [header stuff] -d '{"key":"value"}' [host]
成NSMutableUrlRequest。到目前爲止,我已經剝離了什麼工作正常,只保留導致我麻煩的事情,即-d'{「key」:「value」}'。另一個頭部分很好。
根據捲曲手冊-d意味着該有效載荷在應用/ X WWW的form-urlencoded格式發送的,所以我做了以下:
NSString* post = @"{\"key\":\"value\"}";
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[_request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[_request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[_request setHTTPMethod:@"POST"];
[_request setHTTPBody:postData];
這將返回以下錯誤
失敗,錯誤錯誤域= AFNetworkingErrorDomain代碼= -1011「預期的狀態代碼在(200-299),得到400」UserInfo = 0xa363550 {NSLocalizedRecoverySuggestion = {「代碼」:107,「錯誤」:「壞www-form-urlencoded數據「}
任何一個人都可以指向正確的方向調試這個東西? -A
你完全正確!它現在像一個魅力:) – Alex
@亞歷克斯它是哪一個? – 2013-02-01 14:47:01
解決方案適用於那些感興趣的人[_request setValue:@「application/json」forHTTPHeaderField:@「Content-Type」]; – Alex