gpt4 book ai didi

ios - 将curl命令翻译成NSMutableUrlRequest

转载 作者:行者123 更新时间:2023-11-29 04:09:27 25 4
gpt4 key购买 nike

我正在尝试移植一个curl请求:

curl -X POST -H [header stuff]  -d '{"key":"value"}' [host] 

进入 NSMutableUrlRequest。到目前为止,我已经删除了运行良好的内容,只保留了给我带来麻烦的内容,即 -d '{"key":"value"}'。其他 header 部分很好。

根据curl手册-d表示有效负载以application/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={“code”:107,“error”:“bad www-form -urlencoded 数据"}

有人能给我指出调试这个东西的正确方向吗?-A

最佳答案

Content-Type 应该是 application/json 或者可能是 text/plain 。如果其他一切都失败,请尝试 application/octet-stream (原始二进制数据)。

关于ios - 将curl命令翻译成NSMutableUrlRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14648159/

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