【发布时间】:2016-04-26 10:38:47
【问题描述】:
我正在使用 REST API,其中一个 api 是 PATCH,我尝试使用以下代码调用它,但它抛出错误,而相同的 api 在 Postman 客户端中工作。
+ (NSURLSessionTask *)callPATCHAPIWithAPIName:(NSString *)apiName andCompletionHandler:(void(^)(id result, NSInteger responseCode, NSError *error))completionHandler
{
NSString *getURL = @"192.168.1.100/UpdateDeviceInfo/iPhone6/OS 9.2";
NSURL *URL = [NSURL URLWithString:getURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.timeoutIntervalForRequest = 45.0f;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
// Create Data from request
NSData *requestData = [NSData dataWithBytes: [@"" UTF8String] length:[@"" length]];
[request setHTTPMethod:@"PATCH"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSInteger responseCode = [httpResponse statusCode];
NSLog(@"response Code : %ld",(long)responseCode);
}];
[task resume];
return task;
}
[错误 :Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x14e86d490 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}]
我在查询字符串中传递参数,而不是在请求正文中。虽然同样的事情在Postman 客户端和 Android 中工作。
【问题讨论】:
-
我问了一个类似的问题here。我也无法将 Postman 请求转换为 Objective-C 代码。你介意看看我的问题吗?
-
当然@fi12,会调查的
-
感谢您的帮助。
标签: ios objective-c http postman nsmutableurlrequest