3

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"

  • 1
    It'd be really helpful if you said what the actual problem is. You can't post? Or are you getting an error back? What is the error? – Michael Dautermann Sep 02 '13 at 11:45
  • @MichaelDautermann- the problem is that i m not getting the response back the right response – Sakshi Singh Sep 02 '13 at 11:48
  • the server side guy is saying it entering into catch block thats why it is printing "msg= Server encountered error" – Sakshi Singh Sep 02 '13 at 11:49
  • what is the error you are getting?Or you are facing some any other issue?Please let us know where you got stuck. – Apoorv Sep 02 '13 at 11:49
  • Please let me know your Content-Type in header? – Apoorv Sep 02 '13 at 11:50
  • @Apoorv- i have pasted all the code nothing in header – Sakshi Singh Sep 02 '13 at 11:51
  • @SakshiSingh when you create a POST request there has been Header and Body in the request.Please consult you server team and confirm which parameter are in Header and which are in Body of the Request – Apoorv Sep 02 '13 at 12:01
  • It would probably much easier to use `NSURLConnection` directly, together with some knowledge of HTTP. It seems to me, AFNetworking is just adding more and more abstraction layers which do not make the actual task easier, but just different or even more confusing. Test with `curl` tool on the command line: `curl -sSv -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '' http://yourservice/url` – CouchDeveloper Sep 02 '13 at 12:01
  • I tried this request using `NSURLConnection`.I also got the same output. It is not a problem with your code. Not a request error also. Server sending that data as output. Check with server side. Why they are sending that result – Anil Varghese Sep 02 '13 at 12:11
  • @SakshiSingh I have check your request and it seems that there is some issue at the server side.Also please also confirm your credentials you are passing. – Apoorv Sep 02 '13 at 12:15

1 Answers1

1

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.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • @couchDeveloper- So whats is the solution of the problem...? is their any problem from server side.....? – Sakshi Singh Sep 03 '13 at 04:36
  • Well, you should test whether there is a problem. Is your web service working as described, and according the rules for HTTP (and possible REST)? For example, for a GET request, does it return status code 200 (OK) and is the Content-Type (for body data) "application/json"? Does it return 401 when not authenticated? Is the error response in JSON? When the server works as expected, in your code setup the request correctly and try from your network code. But instead passing some foundation object which should be interpreted by AFN as JSON, create the JSON yourself and send that as POST data. – CouchDeveloper Sep 03 '13 at 15:33
  • @CouchDeveloper- Can you test the webservice is it working or not – Sakshi Singh Sep 05 '13 at 04:45
  • @SakshiSingh Trying with `$ curl -sSv -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"id":shail@gmail.com,"pass":"shail"}' http://vinipost.com/Services/Mobile_Application/wcfService.svc/logIn`, returned: `* upload completely sent off: 37 out of 37 bytes < HTTP/1.1 405 Method Not Allowed < Allow: GET < Content-Length: 1565 < Content-Type: text/html; charset=UTF-8 < Server: Microsoft-IIS/7.5 < X-Powered-By: ASP.NET < Date: Thu, 05 Sep 2013 05:41:34 GMT < ` – CouchDeveloper Sep 05 '13 at 05:46
  • This means, for the given resource specified in the URL, the method "POST" is not allowed - only "GET" is allowed. You should consult the documentation, how to authenticate. Usually, you try to connect with a normal request *without* credentials or auth headers. If the resource requires authentication the server sends status code 401 which contains auth headers specifying the "challenge" (e.g. the authorization method). NSURLConnection can handle that nicely. Possibly implement `connection:willSendRequestForAuthenticationChallenge` delegate method. – CouchDeveloper Sep 05 '13 at 05:56