Search code examples
iosobjective-cafnetworking-2nsurlrequest

AFNetworking sending URL as post parameter


I am calling an API in which I am sending a URL link as a post parameter with it. But while converting it into JSON data using

NSData* jsonData  = [NSJSONSerialization dataWithJSONObject:lParameters options:NSJSONWritingPrettyPrinted error:&error];

it adds extra '\' character into the link. and when I log that data string, it logs like

data string : {
  "id" : "1",
  "photoLink" : "https:\/\/7.7.100.120:8443\/webresource\/carsevent\/gallery\/approved\/image-a31ea5e0-6284-402a-9e6a-b0cdba37bc1f.png"
}

Log :

Dictionary :

{
    id = 1;
    photoLink = "https://7.7.100.120:8443/webresource/carsevent/gallery/approved/image-a31ea5e0-6284-402a-9e6a-b0cdba37bc1f.png";
}

So API is returning error in this case. How can I overcome from this?

Any help would be much appreciable. Thanks


Solution

  • JSON must have certain characters escaped with a "\" character and even though "/" is not required to be escaped it is allowed to be escaped. Therefor the JSON with escaped "/" characters is valid and should be accepted by the API.

    You can remove them if needed.

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
    jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    

    File a bug report: http://bugreport.apple.com requesting an option to not escape "/".

    See this SO answer.

    See JSON for the characters that must be escaped.