For a REST web service call using PUT, I need to send JSON that looks like this:
{"leader":"John Smith","leader_id":"asldfkj234234324asldkfs234","resource_uri":"/api/event/38001"}
Instead when I serialize the JSON it inserts extra ‘/’ characters which is why I believe I am getting a 400 error:
{"leader":"John Smith","leader_id":"asldfkj234234324asldkfs234","resource_uri":"\/api\/event\/38001"}
Here is the code I am using. Any ideas?
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
self.clubToEdit.leader, @"leader",
self.clubToEdit.leaderID, @"leader_id",
@"/api/event/38001", @"resource_uri",
nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDictionary options:0 error:&error];
if (!jsonData)
{
NSAssert(FALSE, @"Unable to serialize JSON from NSDict to NSData");
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
RKParams *params = [RKRequestSerialization serializationWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]MIMEType:RKMIMETypeJSON];
[client put:@"/api/event/?format=json&username=test&api_key=apikey" params:params delegate:self];
}
First, your question doesn’t match the code. In your question, you state that you need to send
"\api\event\38001"whereas in your code you write “/api/event/38001". Since your question is only meaningful with the second case, I’ll assume it to be effective.Now the thing is that your JSON serializer class escapes the slashes (some special characters need escaping) and that’s why it adds the backslashes before those characters.