我正在使用REST API,其中一個API是PATCH
,我試圖使用以下代碼調用它,但它引發錯誤,而同一個API在Postman
客戶端中工作。在iOS中發送HTTP PATCH請求(Objective-c)
+ (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;
}
[錯誤:錯誤域= NSURLErrorDomain代碼= -1002 「不支持URL」 的UserInfo = {NSLocalizedDescription =不受支持的URL,NSUnderlyingError = 0x14e86d490 {錯誤域= kCFErrorDomainCFNetwork代碼= -1002 「(空)」} }]
我傳遞參數在查詢字符串,而不是在請求正文。儘管同樣的東西在Postman
客戶端& Android中工作。
我問了一個類似的問題[here](https://stackoverflow.com/questions/48940176/status-code-400-when-converting-postman-request-into-objective-c?noredirect=1#comment84887541_48940176) 。我也無法將郵遞員請求轉換爲Objective-C代碼。你介意看看我的問題嗎? – fi12
當然@ fi12,會調查它 –
感謝您的幫助。 – fi12