Sakshi Singh
Sakshi Singh

Reputation: 65

how to post json data using AFnetworking or NSJsonserialization

I have used GET a number of time. but in current situation i have to use a webService using POST method. i have gone through many of the tutorials but unable to do. the path is "http://vinipost.com/Services/Mobile_Application/wcfService.svc/logIn" and the paramenters are "id" and "pass" the email id for testing is "shail@gmail.com" and the password is "shail"

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://vinipost.com/Services/Mobile_Application/wcfService.svc/logIn"]];
    [httpClient setParameterEncoding:AFFormURLParameterEncoding];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"http://vinipost.com/Services/Mobile_Application/wcfService.svc/logIn" parameters:@{@"id":@"shail@gmail.com",@"pass":@"shail"}];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = [JSON objectForKey:@"logInResult"];
         NSLog(@"=========%@",self.movies); 
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);

}];
[operation start];

OUTPUT

=========(
        {
        msg = "Server encountered error";
    }
)

the server side guy is saying it entering into catch block thats why it is printing "msg= Server encountered error"

Upvotes: 3

Views: 754

Answers (1)

CouchDeveloper
CouchDeveloper

Reputation: 19164

I would like to elaborate on my comment, and probably provide a few hints how to track down possible issues with web services and APIs.

Ensure the web service is working as expected. You don't need Xcode - but an invaluable command line tool curl. curl is powerful - and it lets you perform HTTP requests in a very concise manner on the command line, despite the many options it provides may look confusing.

(If you need specific help about curl, please consult the man page and the web).

So, lets try curl, start your Terminal app and lets enter a simple GET command to test whether curl is working:

$ curl -X GET http://example.com

curl should return a http page from www.example.com on the console.

In order to get help from curl, open the man page:

$ man curl

You can browse the content. Note: curl is sophisticated - not complex. Don't get overwhelmed - start with the basics:

The following POST request from your issue above is more elaborate then the simple GET. We want to POST a JSON.. First we need our JSON: which is

{"id": "shail.@gmail.com", "pass": "shail"}

Now enter in the command line:

$ curl -X POST -H "Content-Type: application/json" -d '{"id": "shail.@gmail.com", "pass": "shail"}' "http://vinipost.com/Services/Mobile_Application/wcfService.svc/logIn"

Note: when sending JSON we SHOULD inform the server about the Content-Type. Well according the HTTP rules, we really should, what simply means: DO send a Content-Type!

The appropriate Content-Type is "application/json" - as it is specified in the command.

When your service is working it should return "something". We can even tell our preferred kind of response data, say we want a response body in JSON back. To tell that the server, include an appropriate header:

"Accept: application/json"

we could be even more specific about the character encoding:

"Accept: application/json; charset=utf-8"

You need to be picky about the syntax!

Now, what we expect is a JSON response in UTF-8

$ curl -X POST -H "Content-Type: application/json" -H "Accept: application/json; charset=utf-8" -d '{"id": "shail.@gmail.com", "pass": "shail"}' "http://vinipost.com/Services/Mobile_Application/wcfService.svc/logIn" 

The response might be printed to the console - but not pretty.

Here's a solution:

$ <curl command> | python -m json.tool

even better:

$ curl -sSv <other commands>  | python -m json.tool

Now, we use the powerful pipe command (|) to invoke a second tool (python) which starts a python program which takes the output from curl and pretty prints the JSON to the console.

Now, once you have verified that the web service is working, mapping the curl command to a corresponding NSURLConnection request is more than simple.

You should use NSJSONSerialization to create the JSON from a dictionary and serialize it to a NSData object to a JSON (text).

Setup a NSMutableRequest object as follows:

Set the headers "Content-Type" and "Accept" as above. Assign the HTTPBody property the JSON data (which is UTF-8 encoded). Set the URL, and method "POST".

Start the connection.

The last step depends. You can use a convenient method from NSURLConnection, say sendAsynchronousRequest:queue:completionHandler: - or use the delegate methods, or use a third party library, etc.

Upvotes: 1

Related Questions