Calling Azure Function app works in console but not in xamarin forms

Viewed 289

So I created a function which is a Get request. All the function does is retrieves some data from an SQL database and returns it.

I call this function in a console app, just to test it, and all is working as it should. I then copy the code and paste it into my xamarin.forms app for android. When I run it it immediately gives me a 404 Not found response.

Any idea why it's not working in the xamarin app but is working in the console app?

using (var client = new HttpClient())
        using (var request = new HttpRequestMessage(HttpMethod.Get, "http://{MySite}.azurewebsites.net/api/{myfunction}/"))
        using (var httpContent = CreateHttpContent(new DiaryDate() { Date = DateTime.Today }))
        {
            request.Content = httpContent;
            
            using (var response = await client
                .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                .ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    // do something
                }
            }
        }

CreateHttpContent just converts the given object to JSON.

1 Answers

Update the URL to https scheme (https://{MySite}.azurewebsites.net/api/{myfunction}/). Firstly, there is no reason for choosing non-secure channel. Azure Function side won't require any change to use https. Secondly, as pointed out in the other answer, there is restriction in client side for http which would require tweak to unblock.

UPDATE: In this particular call, you are using Http GET, but sending a request content (json of DiaryDate object). So, it will send that via query string in the the URL. Though I am not yet sure how it's working in console app (might be something to do with how it's handled in Xamarin/Mono http handler v/s that of .net core in console app in GET request), but it should be POST surely. Can you change HttpMethod.Get to HttpMethod.Post in the line where you are creating HttpRequestMessage.

Related