问题描述
我正在使用REST API,其中一个api是 PATCH
,我试图使用以下代码调用它,但它会抛出错误,而同样的api在<$ c中工作$ c> Postman client。
I am consuming REST APIs, one of the api is PATCH
, i tried to invoke it using following code but it throws error, while same api works in Postman
client.
+ (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;
}
我在查询字符串中传递参数,而不是在请求正文中。虽然同样的事情是在 Postman
客户端& Android。
I am passing parameters in query string, not in request body. Though same thing is working in Postman
client & Android.
推荐答案
好像你在 NSString中有一个空格* getURL = @192.168.1.100 / UpdateDeviceInfo / iPhone6 / OS 9.2
我不相信网址格式支持空格!相反,如果需要空格,请确保使用 getURL = [getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
以便处理url字符串中的空格!
I do not believe whitespaces are supported in url formats! Instead, if whitespaces are necessary, make sure you use getURL = [getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
in order to handle whitespaces in url strings!
这篇关于在iOS中发送HTTP PATCH请求(Objective-c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!