1

I am trying to POST parameters through the request, to a service that returns a JSON object. The service works well for android and iOS. I am trying to get this working for wp7. The service requires the content type to be 'application/json' I have pasted the code that sets up the http request below:

            var client = new RestClient(baseurl);
            var request = new RestRequest();
            request.Resource = "login";
            request.Method = Method.POST;
            request.AddHeader("Accept", "application/json");
            request.AddHeader("content-type", "application/json");         
            request.RequestFormat = DataFormat.Json;

            var postData = new Dictionary<string, string>()
            {
                {"key1",value1},
                {"key2",value2}
            };

            request.AddBody(postData); 
            client.ExecuteAsync(request, response =>
            {
                var jsonUser = response.Content;
            });

The response error I get from the server is an internal server error. Is anything wrong with the code above. I also tried request.AddParameter method but ended with the same result. The code for that is below:

            var client = new RestClient(baseurl);
            var request = new RestRequest();
            request.Resource = "login";
            request.Method = Method.POST;
            request.AddHeader("Accept", "application/json");
            request.AddHeader("content-type", "application/json");         
            request.RequestFormat = DataFormat.Json;

            var postData = new Dictionary<string, string>()
            {
                {"key1",value1},
                {"key2",value2}
            };
            var json = JsonConvert.SerializeObject(postData);
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            client.ExecuteAsync(request, response =>
            {
                var jsonUser = response.Content;
            });

Is there anything that I am doing wrong in either of the cases?

Pedro Lamas
  • 7,185
  • 4
  • 27
  • 35
  • Your #1 approach seems ok to me... can you install and open Fiddler, open the Emulator (Fiddler has to be running **before** you open the Emulator) and check if the request is being made correctly? – Pedro Lamas Sep 19 '12 at 10:09
  • Thank you Pedro for your response. I will try that. In the meantime while I figure that out. Could you tell me if the code, request.AddParameter("key1", value1, ParameterType.GetOrPost); request.AddParameter("key2", value2, ParameterType.GetOrPost); does the same thing. As in, does it send the parameters as part of HTTP request body. I don't want to send the params as query string. Thanks. – Ashton D'Sa Sep 19 '12 at 11:26
  • Using that will get your values in the Post, not as query string, but it won't be JSON encoded if that is what you need! – Pedro Lamas Sep 19 '12 at 12:08
  • try `request.AddParameter("json", json);` – Derek Beattie Sep 19 '12 at 14:41
  • Thanks guys..it worked with request.AddParameter("key1", value1, ParameterType.GetOrPost); The service was looking for it without the json encoding. This was the link that led me to the right solution http://stackoverflow.com/questions/6312970/restsharp-json-parameter-posting. And also Fiddler didn't quite work well with Windows emulator. It may need some setting to get working with the emulator. But WireShark worked fine. Thanks Pedro and Derek for your responses. – Ashton D'Sa Sep 20 '12 at 04:46

0 Answers0