在 iOS 中发送 HTTP PATCH 请求(Objective

编程入门 行业动态 更新时间:2024-10-26 12:24:47
本文介绍了在 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

本文发布于:2023-10-12 16:29:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485271.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HTTP   iOS   Objective   PATCH

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!