开发者

NSMutableURLRequest: Can't get body to add with PUT request

开发者 https://www.devze.com 2023-03-05 05:59 出处:网络
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

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"];
    开发者_如何学Go [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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消