2

I’m familiar with iOS/Xcode development and now trying to develop my own web services. Unfortunately I’m struggling with the best way to pass a JSON string from my iOS application to the PHP page.

In xcode I create a JSON string similar to:

{"email" : "",  "name" : "Test Rest",}

I’m then creating a URL (GET) :

//localhost/index.php?method=createRestaurant&json={"email" : "",  "name" : "Test Rest",}

I’m trying to use NetBeans to debug the PHP but both NetBeans & iOS fail with either bad URL or invalid character error, and from what I’m guessing it’s the JSON characters that are failing {}.

If I use Chrome PostMan and issue the same URL it works which proves that the PHP is work / doing what I need it to do.

So in summary what is the best way to include the JSON data in the URL is the something that needs changing in XCode that affects the way the JSON is encoded?

  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myDictionary options:NSJSONWritingPrettyPrinted  error:&error];
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

In an ideal world I should be using Java but due to speed, environment costs etc I’m attempting to get my system working using PHP. Once the solution is complete I’ll then work on porting the solution to Java.

Thanks for any help.

4 Answers 4

1

A couple of thoughts:

  1. Your JSON isn't valid. You have an extra comma in there. Check it out with JSON Lint.

  2. You should percent escape your URL parameters because characters like spaces are not permitted in a URL. You can use stringByAddingPercentEscapesUsingEncoding. Or better, if your string might have some other valid URL characters that can't be used within an individual URL parameter, you can use the Core Foundation CFURLCreateStringByAddingPercentEscapes, which gives you more control. I use a NSString category that provides a simple interface to this CoreFoundation function:

    @implementation NSString (UrlEncode)
    
    + (NSString *)stringByAddingPercentEscapesFor:(NSString *)string
                    legalURLCharactersToBeEscaped:(NSString *)legalURLCharactersToBeEscaped
                                    usingEncoding:(NSStringEncoding)encoding
    {
        return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                         (CFStringRef)string,
                                                                         NULL,
                                                                         (CFStringRef)legalURLCharactersToBeEscaped,
                                                                         CFStringConvertNSStringEncodingToEncoding(encoding)
                                                                         ));
    }
    
    - (NSString *)stringByAddingPercentEscapesFor:(NSString *)legalURLCharactersToBeEscaped
                                    usingEncoding:(NSStringEncoding)encoding
    {
        return [NSString stringByAddingPercentEscapesFor:self
                           legalURLCharactersToBeEscaped:legalURLCharactersToBeEscaped
                                           usingEncoding:encoding];
    }
    
    - (NSString *)stringByAddingPercentEscapesForURLParameterUsingEncoding:(NSStringEncoding)encoding
    {
        return [NSString stringByAddingPercentEscapesFor:self
                           legalURLCharactersToBeEscaped:@";/?:@&=+$,"
                                           usingEncoding:encoding];
    }
    
    @end
    

    So, you could use stringByAddingPercentEscapesForURLParameterUsingEncoding on your JSON string before adding it to your URL. Or you could call CFURLCreateStringByAddingPercentEscapes yourself.

Sign up to request clarification or add additional context in comments.

Comments

0

You can either past data by GET request by changing URL like this:

//localhost/index.php?method=createRestaurant&email=emailData&name=TestData

or you can send the data by POST request in a POST variable.

Comments

0

I would use the POST method and then simply print it out in your php script for debugging.

Debug output would be something like this:

$json = json_decode($_POST['json']); print_r( $json );

Comments

0

Thank you for all your suggestions, I've now changed from GET to POST and all is working well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.