Naila
Naila

Reputation: 280

Xamarin Android Httppost Request

I am trying to make a post call to my web Api which is on local host. But I am getting following error

result = {System.Net.WebException: The remote server returned an error: (415) Unsupported Media Type. at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult asyncResult) [0x0005e] in /Users/builder/data/lanes/3511/f4db8a57/source/mono/mcs/class/System/Sy...

Can anyone help? Below is my code:

private void click (Object sender, EventArgs e)
    {
        UserInfo user = new UserInfo(1, "[email protected]", "helloss");
        String data = JsonConvert.SerializeObject(user);
        WebClient wc = new WebClient();
        wc.UploadStringAsync(new Uri("http://192.168.206.2:155/api/register"), data);
        wc.UploadStringCompleted += Wc_UploadStringCompleted;
    }

    private void Wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        var result = e.Error;
    }

Upvotes: 0

Views: 596

Answers (2)

Kai Brummund
Kai Brummund

Reputation: 3568

So far, the Server only get's a string from you and can't know if, it is json, xml or something else. You just have to tell him.

You can to this on a WebClient via:

wc.Add("Content-Type", "aplication/json");

If you are using the HttpClient you have to set ist via the Content property:

request.Content = new StringContent("json", Encoding.UTF8, "application/json");

Upvotes: 1

Shohn
Shohn

Reputation: 967

The HTTP specification states:

415 Unsupported Media Type The 415 (Unsupported Media Type) status code indicates that the origin server is refusing to service the request because the payload is in a format not supported by the target resource for this method. The format problem might be due to the request's indicated Content- Type or Content-Encoding, or as a result of inspecting the data directly.

It seems your server is having troubles with the format... try adding the headers with .Add("Accept", "aplication/json");

Upvotes: 0

Related Questions