Pregunta

I am JSON newbie and can't find any material on how to simulate JSON payload request.

My ultimate goal is to be able to build an objective-c iOS app that will handle these request-response. I am aware of ASIHttprequest framework and the request-response mechanism it works around.

However right now I have a webservice api which expects various json payloads and also provides response in json format. Here is an example:

Example URL:

https://mywebServiceURL/api/ApiKey/user/create

The ContentType header = “application/json”.

Payload: The PUT payload is a JSON dictionary, containing the following keyvalue pairs:

email
screenName
User’s screen name. 
password
passwordConfirm    
phoneNumber (optional)
User’s phone number (optional)
picture A png file (64x64), encoded as a Base64 string (optional)

Now my questions:

1 - how do I simulate this normally (outside ios, just for the sake of testing)? I searched google but can't find exactly what I need, I got curl.exe but it gives me same as what a browser gives, like method not allowed etc. But that's not the only thing I want. I want to play with various requests, supply values and take the api for a ride for sometime before I know how it really works for PUT, GET, POST etc.

2 - what is the best way to incorporate such stuff into iOS? I already have ASIHttp for web requests and JSONKit for JSON handling included in my project.

I have done this kind of stuff but with xml responses, get requests. JSON I am working for the first time. Just a pointer to an example stuff like this would be a great help.

¿Fue útil?

Solución

  1. There are several Chrome extensions, such as Advanced REST client or REST Console, that you can use to simulate REST calls. These are very quick and easy to install into your browser and will allow you to construct requests and view the response.

  2. I recommend using the open source iOS networking library AFNetworking. This library has built in support for REST calls and JSON parsing. It is very easy to get up and running, and is very powerful if you need more advanced features.

Hope this helps.

Otros consejos

Jsut to add upon Andy's suggestions as I have used similar kind of things in one of my recent app.

  1. Used Chrome extension Poster for testing REST calls.
  2. Used ASIHttpRequest for handling async queries.

Sample code snippet

    //dictionaryData contains the login user details
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryData  options:kNilOptions error:nil]; 
    NSString *jsonString = [[[NSString alloc] initWithData:jsonData                  encoding:NSUTF8StringEncoding]autorelease];                                         
    //Handling ASI requests
    ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:uri];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"Accept" value:@"application/json"];
    [request appendPostData:[jsonData dataUsingEncoding:NSUTF8StringEncoding]];
    [request setRequestMethod:@"POST"];
    [ASIHTTPRequest setDefaultTimeOutSeconds:30];
    request.delegate = self;
    [request startAsynchronous];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top