REST POST not responding in Xamarin.Android App. It is responding with Postman but not app.

901 views Asked by At

I am sending POST request from Xamarin.Android appl on VS 2015 using RestSharp but no response is arriving. I increased timeout but no response. IT IS WORKING WITH POSTMAN but not with Android app.

 RestClient client = new RestClient("https://" + orgId + ".internetofthings.ibmcloud.com");
 RestRequest request = new RestRequest("/api/v0002/application/types/" + typeId + "/devices/" + deviceId + "/events/" + typeId + deviceId, Method.POST);

        byte[] byteArray = Encoding.UTF8.GetBytes(username + ":" + password);
        string authenticationToken = Convert.ToBase64String(byteArray);

        request.AddHeader("Authorization", "Basic " + authenticationToken);
        request.Timeout = 2000000;
        request.AddJsonBody(newVal);


        IRestResponse response = client.Execute(request);
        return response.StatusCode.ToString();`

I am sending only "newVal" (string) in the body. This works with Postman. Please let me know if more explanation is required. THANK YOU SO MUCH.

Environment: Visual Studio 2015, RESTSharp, Xamarin.Android, Server: IBM BlueMix

2

There are 2 answers

0
TalhaAhmed On BEST ANSWER

This is resolved, Issue was with the 'content type'. I was adding content type as:

request.AddHeader("Content-Type","application/json");

but Content type was not setting for some reason and that's why my request was being shut from server and returned status code 0. So instead, I did the following and it worked fine.

request.JsonSerializer.ContentType = "application/json; charset=utf-8";
0
idan On

not sure where this is failing by might be that you don't pass the header correctly or the body message doesn't have the correct JSON format. You can check that on below link and make sure it is correct

https://docs.internetofthings.ibmcloud.com/swagger/v0002.html#/