NSMutableURLRequest:无法使用 PUT 请求添加正文
我正在尝试执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了这个问题。我添加了一个 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.