1

I am Trying to Update data through NSUrlConnection using PUT method. For adding data i am using POST method it's working fine but the same time PUT it's not working.

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@update/%@?userId=%@",xapp.urlString,[dict valueForKey:@"id"],[profileDict valueForKey:@"id"]] ];
        NSError *error;
        NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"PUT"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:jsonData];
    updateHiveConnection=[NSURLConnection connectionWithRequest:request delegate:self];

I am sending JSON Data to this api {"id":3,"active":false} after that i am getting error

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0x1fb456f0 {NSDebugDescription=No value.}

Any help would be appreciated thanks

Chithri Ajay
  • 907
  • 3
  • 16
  • 37

2 Answers2

4

I had the same problem and after some google search I've found out that adding the following line of code to the NSMutableURLRequest *req will do it:

[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

Found this on Jeff LaMarche blog: http://iphonedevelopment.blogspot.ro/2008/06/http-put-and-nsmutableurlrequest.html

alexbumbu
  • 321
  • 3
  • 10
1

So what it looks like to me is that you don't actually have JSON.

Are you Sure this have values in it:

xapp.urlString,[dict valueForKey:@"id"],[profileDict valueForKey:@"id"]]

Unless you pass the option NSJSONReadingAllowFragments to [NSJSONSerialization JSONObjectWithData:options:error:] the response from the server must be valid JSON with a top level container which is an array or dictionary.

for example:

{ "response" : "Success" }

P.S. If you want a mutable dictionary you must also include NSJSONReadingMutableContainers in your options.

Vizllx
  • 9,135
  • 1
  • 41
  • 79