gpt4 book ai didi

ios - 使用 NSURLSession 使用原始正文发布请求

转载 作者:行者123 更新时间:2023-12-01 18:09:33 25 4
gpt4 key购买 nike

我已经卡在这里了。下面是我使用 NSURLSession 使用原始正文发布请求的代码。我得到了 response = NULL 并且没有错误。

NSString* stringRequest = @"https://chocudan.com/api/shops/by_ids";
NSURL* urlRequest = [NSURL URLWithString:stringRequest];

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:urlRequest];
request.HTTPMethod = @"POST";

NSString* bodyRequest = @"563c268b84ba489c4729f149";

//I have to tried a base64 convert here but still not work.
//request.HTTPBody = [NSData base64DataFromString:bodyRequest];

request.HTTPBody = [bodyRequest dataUsingEncoding:NSUTF8StringEncoding];


NSURLSessionConfiguration* configureSession = [NSURLSessionConfiguration defaultSessionConfiguration];

configureSession.HTTPAdditionalHeaders = @{@"Content-Type" : @"application/json charset=utf-8",
@"Content-Lenght" : @"180"};

NSURLSession* session = [NSURLSession sessionWithConfiguration:configureSession];

NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSHTTPURLResponse* respHttp = (NSHTTPURLResponse*) response;

if (!error && respHttp.statusCode == 200) {

NSDictionary* respondData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

NSLog(@"%@", respondData);
}else{

NSLog(@"%@", error);
}

}];

[dataTask resume];

我必须尝试使用​​ postman ,一切正常。这是图片。
test with postman

预先感谢。

最佳答案

也试试改

    NSArray* bodyArray = @[@"563c268b84ba489c4729f149"]
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:bodyArray
options:NSJSONWritingPrettyPrinted error:&error];
request.HTTPBody = jsonData;

关于ios - 使用 NSURLSession 使用原始正文发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327919/

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