NSMutableURLRequest:无法使用 PUT 请求添加正文

发布于 2024-11-06 10:30:30 字数 793 浏览 5 评论 0原文

我正在尝试执行 PUT。作为测试,我对一些 JSON 数据执行 GET 请求,并将收到的数据存储在我在其他地方初始化的变量数据中。我能够解码原始数据,一切看起来都很好。当我将其发回时,我会清除我要放入的 URI 的 HTTP 主体中的所有内容。

  data = receivedData:

  NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:putURI]] autorelease];
     [request setTimeoutInterval:10];
     [request setHTTPMethod:@"PUT"];
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
     [request addValue:self.token forHTTPHeaderField:@"Authorization"];
     [request setHTTPBody:data];
     NSLog(@"\nVerify existence of original data packet: \n%@\n\n",data);

     self.putDeviceOnListConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

任何想法有什么问题吗?一如既往,我们非常感谢您的帮助。

I am trying to perform a PUT. As a test, I execute a GET request on some JSON data and store this receivedData in a variable data that I have initialized elsewhere. I am able to decode the original data and everything looks fine. When I send it back I wipe out everything in the HTTP body of the URI I am PUTting to.

  data = receivedData:

  NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:putURI]] autorelease];
     [request setTimeoutInterval:10];
     [request setHTTPMethod:@"PUT"];
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
     [request addValue:self.token forHTTPHeaderField:@"Authorization"];
     [request setHTTPBody:data];
     NSLog(@"\nVerify existence of original data packet: \n%@\n\n",data);

     self.putDeviceOnListConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

Any ideas what is wrong? Your help is greatly appreciated, as always.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2024-11-13 10:30:30

我发现了这个问题。我添加了一个 addValue:forHTTPHeaderField 来表示数据包的长度,并将内容类型更改为“application/x-www-form-urlencoded”。

I have found the problem with this. I have added a addValue:forHTTPHeaderField for for the length of the data packet and changed the content-type to "application/x-www-form-urlencoded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文