0

I have to pass JSON dictionary as POST data to a Webservice. One key involves an Amazon S3 URL string. The sample request json which works has the URL as....

https:\/\/myappbucket.s3.amazonaws.com\/2014230407_102323.jpg?response-content-type=image\/png&Signature=123456%3D&Expires=139756222548&AWSAccessKeyId=ABCDEF

Notice the backslashes just before the forwardslashes? I have never seen a URL like that, but thats how I'm supposed to pass it.

I tried

stringByAddingPercentEscapesUsingEncoding

and

stringByReplacingPercentEscapesUsingEncoding

while using NSASCIIStringEncoding and NSUTF8StringEncoding

Can anyone make sense of this?

Akash Malhotra
  • 1,106
  • 1
  • 17
  • 30
  • possible duplicate of [JSON: why are forward slashes escaped?](http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped). tl;dr: they escape the forward slashes. Mildly unnecessary, but good to have for most embedded JSON. – CodaFi Apr 16 '14 at 05:42
  • So what you are saying is the backslashes are not needed? – Akash Malhotra Apr 16 '14 at 05:47
  • JSON doesn't require that you escape slashes like that. The server may be using them for other purposes, or may be based on a library or language that escapes by default. – CodaFi Apr 16 '14 at 06:09

2 Answers2

1

if we try to convert url into legal url trough stringByAddingPercentEscapesUsingEncoding than it adds all percent escapes necessary to convert the receiver into a legal URL string.Uses the given encoding to determine the correct percent escapes. if we use stringByReplacingPercentEscapesUsingEncoding than it replaces all percent escapes with the matching characters as determined by the given encoding. mostly to get valid url, we can use NSUTF8StringEncoding to remove backslashes just before the forwardslashes in url.

Ravi Tailor
  • 189
  • 4
  • It's simply *not possible* to "convert" some improperly encoded URL using `stringByAddingPercentEscapesUsingEncoding` into a "valid" URL. A URI has several *components*, and each component requires a *different* percent encoding algorithm (see RFC 3968). So, your proposed approach won't work. In fact, a workable and reliable approach is (much) more elaborated. Before iOS 7.0, there was no API which made this simpler. Since iOS 7.0 there is `NSURLComponents`. Have a look! ;) – CouchDeveloper Apr 16 '14 at 09:29
0

Generally, you should use a JSON serializer library (e.g. NSJSONSerialization) in order to obtain a JSON from a JSON representation and not try to create the JSON yourself.

A JSON representation is a NSDictionary or NSArray object containing other objects which recursively represent your JSON. Your URL will be represented as a NSString.

What you need to do is simply have a valid URL as a NSString, properly encoded according RFC 3968 and assign it the JSON representation, e.g.:

NSURL* url = ...;
NSDictionary* jsonObject = @{@"url": [url path]};

Now, you can serialize the JSON representation to a JSON:

NSError* error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonObejct 
                                                   options:0 
                                                     error:&error];

That's it, and you don't bother how the JSON encoded string looks like (encapsulated in the NSData object as a UTF-8 character sequence).

Purposefully, when you POST this JSON, you SHOULD specify a corresponding request header:

ContentType: application/json

which lets you just use the JSON data as body data as is:

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
request.HTTPBody = jsonData;

Side note: [url path] returns a URL as a string according RFC 1808 which is obsoleted by RFC 3968 since January 2005 already. Today, there are newer APIs since iOS 7.0, see NSURLComponents.

Community
  • 1
  • 1
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67