send HTTP PATCH request in iOS (Objective-c)(在 iOS 中发送 HTTP PATCH 请求(Objective-c))
问题描述
我正在使用 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];//根据请求创建数据NSData *requestData = [NSData dataWithBytes: [@"" UTF8String] 长度:[@"" length]];[请求 setHTTPMethod:@"PATCH"];[请求 setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestData length]] forHTTPHeaderField:@"Content-Length"];[请求 setHTTPBody:requestData];NSURLSessionDataTask *task = [会话 dataTaskWithRequest:request完成处理程序:^(NSData *data, NSURLResponse *response, NSError *error){NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) 响应;NSInteger responseCode = [httpResponse statusCode];NSLog(@"响应代码:%ld",(long)responseCode);}];【任务简历】;返回任务;}
<块引用>
[错误 :Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x14e86d490 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}]
我在查询字符串中传递参数,而不是在请求正文中.虽然同样的事情在 Postman
客户端 &安卓.
你的 NSString *getURL = @"192.168.1.100/UpdateDeviceInfo/iPhone6/OS 9.2" 中好像有空格
我认为 url 格式不支持空格!相反,如果需要空格,请确保使用 getURL = [getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
来处理 url 字符串中的空格!
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;
}
[Error :Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x14e86d490 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}]
I am passing parameters in query string, not in request body. Though same thing is working in Postman
client & Android.
It seems like you have a whitespace in NSString *getURL = @"192.168.1.100/UpdateDeviceInfo/iPhone6/OS 9.2"
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 iOS 中发送 HTTP PATCH 请求(Objective-c)
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01