2

Getting error while posting data to sql using .net Web API in xamarin.forms

StatusCode: 204, ReasonPhrase: 'No Content', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{Cache-Control: no-cache Pragma: no-cache Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Date: Thu, 17 Mar 2016 08:32:28 GMT Expires: -1 }}

this is my code to post data

 T returnResult = default(T);
        HttpClient client = null;
        try
        {
            client = new HttpClient();
            client.BaseAddress = new Uri(HostName);
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.Timeout = new TimeSpan(0, 0, 15);
            HttpResponseMessage result = null;
            StringContent data = null;
            if (content != null)
                //     data = new StringContent(JsonConvert.SerializeObject(content), UTF8Encoding.UTF8, "application/json");
                data = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");
            if (method == HttpMethod.Get)
                result = await client.GetAsync(endpoint);
            if (method == HttpMethod.Put)
                result = await client.PutAsync(endpoint, data);
            if (method == HttpMethod.Delete)
                result = await client.DeleteAsync(endpoint);
            if (method == HttpMethod.Post)
                result = await client.PostAsync(endpoint, data);
            if (result != null)
            {
                if (result.IsSuccessStatusCode
                                   && result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var json = result.Content.ReadAsStringAsync().Result;
                    returnResult = JsonConvert.DeserializeObject<T>(json);
                }
            }

where should be the problem ?. My API is working fine and it is enabled CORS

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
shamnad
  • 328
  • 5
  • 21

2 Answers2

3

Here is an example of how I am using PostAsync in my code, I think the error 'No Content' is referring on you are not sending anything to the server maybe? I hope this example helps:

public async Task<bool> PostAppointmet(AppointmentEntity anAppointment)
{
    try{
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + App.apiToken);

        const string resourceUri = ApiBaseAddress + "/citas";

        string postBody = JsonConvert.SerializeObject(anAppointment);

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = await client.PostAsync (resourceUri, new StringContent (postBody, Encoding.UTF8, "application/json"));

        if (response.IsSuccessStatusCode) {
            return true;
        }

        return false;
    }
    catch{
        return false;
    }
}

Where AppointmentEntity is my Model:

public class AppointmentEntity
{
    public int doctor { get; set; }
    public PatientEntity paciente { get; set; }
    public DateEntity cita { get; set; }...
}
Mario Galván
  • 3,964
  • 6
  • 29
  • 39
  • Thanks for replaying me . I fount that there i am passing data i can see that on debuging . if any one help me to resole my solution that will be better to me . one again thanks for the answer @Mario Galván – shamnad Mar 18 '16 at 09:12
1

Try to use like this

var task = client.PostAsync(endpoint,content:data);

Vishnu PS
  • 35
  • 8
  • My problem was in API which was not coded right any how thanks for replaying me .If any one getting this error again please make sure that the API data posting is working fine . We can use **fiddler** or we have **Postman** in chrome to verify that data is posting. Postman helped me to resolve this issue its simple also . – shamnad Mar 19 '16 at 10:05