Forum Xamarin.Forms

Delete requests (REST API) fails using Xamarin.forms but succeeds using a Console App

Hello, I have the most peculiar problem.

I'm using an HttpClient (System.Net.Http) to communicate with an online server using the REST API on my Xamarin.forms App.
While GET and POST requests works as expected, I'm getting a 400 Bad Request response when sending DELETE requests. Normally I would have suspected a bug of my own creation, but when running the exact same function on a C# Console App, I get a successful response code (and the entry is deleted from the database).
I've created a minimal reproducible example using a new blank Xamarin.forms app with the following MainPage code

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        protected override async void OnAppearing()
        {
            base.OnAppearing();
            HttpClient c = new HttpClient();
            var response = await c.DeleteAsync(SomeRequestUrl);
            await DisplayAlert(response.IsSuccessStatusCode.ToString(), response.StatusCode.ToString(), "ok");
        }
    }

The alert displays "Bad Request" (and the entry remains in the database)

And the following C# Console App:

using System;
using Nito.AsyncEx;
using System.Net.Http;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            AsyncContext.Run(() => MainA());
        }
        static async void MainA()
        {
            HttpClient c = new HttpClient();
            var response = await c.DeleteAsync(SomeRequestUrl);
            Console.WriteLine(response.StatusCode);
        }
    }
}

Which prints "OK" and results in the entry being removed from the database. I've created an example database for this question but as I'm a new user I can't post the link. Any help would be appreciated, thanks

Answers

Sign In or Register to comment.