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
I don't think
"url"
passed toclient.PostAsync
should be a string value. Maybe remove the quotes? I'm guessingurl
is a string variable not shown in your code.405 usually happens with an invalid resource address, which appears to be the case here.
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. =(
Uri
andstring
are two different types.Can you post to the same address using Postman or cURL?
Yes =D
In Postman works.
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):nice, but how is the signature from your web api ?
Do you use [FromBody] ?
No, I've edited my code for clarity.