Forum Xamarin.Forms

Problema POST xamarin forms 405 NOT ALLOWED

I am trying send a post message to my web api, but xamarin is throw a 405 error.
public bool Login(string User, string Pass)
{
try
{
List retorno = new List();
var handler = new NativeMessageHandler();
var client = new HttpClient();
{
Loginn log = new Loginn();

                log.Login = User;
                log.Senha = Pass;

                string content = JsonConvert.SerializeObject(log);
                var requestBody = new StringContent(content);
                requestBody.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                var response = client.PostAsync("url", requestBody).Result;

                if (response.IsSuccessStatusCode)
                {
                    return true;
                }
                return false;
            }

        }
        catch (Exception ex)
        {
            return false;
        }
    }

In response :
{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Tue, 04 Apr 2017 17:57:31 GMT
Allow: POST
Content-Length: 72
Content-Type: application/json; charset=utf-8
}}

Someone can help me ?

WEB API :

    [HttpPost]
    public HttpResponseMessage Autentica(LoginVM login)

Thanks

Posts

  • mikeacostamikeacosta USUniversity ✭✭✭
    edited April 2017

    I don't think "url" passed to client.PostAsync should be a string value. Maybe remove the quotes? I'm guessing url is a string variable not shown in your code.

    405 usually happens with an invalid resource address, which appears to be the case here.

  • LucasNicolettoLucasNicoletto USMember

    Yes.

    I am passed a URI, for example i substitute by url.

    This web api that i call i have many methods get. All Get methods works fine. But this POST does not working. =(

  • mikeacostamikeacosta USUniversity ✭✭✭

    Uri and string are two different types.

    Can you post to the same address using Postman or cURL?

  • LucasNicolettoLucasNicoletto USMember

    Yes =D

    In Postman works.

  • mikeacostamikeacosta USUniversity ✭✭✭
    edited April 2017

    This is what works for me (The client instantiation is from a shared static class where many consumers use the default ApiBaseAddress. I'm overriding it in this particular POST):


  • LucasNicolettoLucasNicoletto USMember

    nice, but how is the signature from your web api ?

    Do you use [FromBody] ?

  • mikeacostamikeacosta USUniversity ✭✭✭
    edited April 2017

    No, I've edited my code for clarity.

Sign In or Register to comment.