404 error on PostAsync on web service

JosecanallaJosecanalla ARMember ✭✭

Hello. I have a web service developed in PHP which receives a POST request with JSON data and saves an entity. I test the web service with Postman and it works OK.

I have a Xamarin.Forms project where I call this webservice, and from my PCL class I have this code:

public async Task GuardarSolicitud(Solicitud solicitud)
        {
            string RestUrl = "http://routetoapi";
            var uri = new Uri(string.Format(RestUrl));

            var json = JsonConvert.SerializeObject(solicitud);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            HttpResponseMessage response = null;
            response = await client.PostAsync(uri, content); //Previously I do client = new HttpClient();   client.MaxResponseContentBufferSize = 256000;

            if (response.IsSuccessStatusCode)
            {
                await DisplayAlert("Ok", "Ok", "Ok");
            }
            else
            {
                await DisplayAlert("Error", response.StatusCode.ToString(), "ok");
            }
        }

In the client.PostAsync() method I get response error 404 with this details:

{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Fri, 17 Mar 2017 17:29:34 GMT
Server: Apache
X-Powered-By: PHP/5.6.30
X-DEBUGKIT-ID: 8c05b10e-a153-4ca3-af6f-04c65381ccba
Keep-Alive: timeout=10, max=200
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
}}

What can I do? Thanks!

Tagged:

Best Answer

  • JosecanallaJosecanalla AR ✭✭
    Accepted Answer

    Well, I found the problem. The Xamarin code is OK, the problem is with the API. I am sending one extra key/value pair in the JSON which is not handled by the API. I don't understand why get 404 error and not 500, but deleting the extra key/value pair I get it working fine.

Answers

  • DanielBarrrosAmorimDanielBarrrosAmorim BRUniversity ✭✭

    Is the Resturl "http://routetoapi" correct? Is that your localhost?

  • JosecanallaJosecanalla ARMember ✭✭

    Yes, it's correct because I try it with postman. No, it isn't localhost. Is a public URL.

  • DanielBarrrosAmorimDanielBarrrosAmorim BRUniversity ✭✭

    It doesn't seem to be a public URL. Your machine or network is probably redirecting to another host.
    Can you open this URL on the browser?
    Are you using the Postman on the same machine?
    On Mac, open the terminal and do a traceroute . If you are on windows, open cmd and try tracert . If possible send the print screen with the results.

  • JosecanallaJosecanalla ARMember ✭✭

    Yes, I can open in a browser (but get a notice because only receipts POST requests). Postman is used in same machine. I also open the URL in the webbrowser of the AVD and get the same notice if I open it in the PC.

    Tracert not works because the URL is http://mydomain.com/api/abonados

    Of course if I do tracert to mydomain.com works OK.

  • JosecanallaJosecanalla ARMember ✭✭

    Also, the GET method for the same URL (with one parameter) works fine from my AVD. The problem is only with the POST method!

  • DanielBarrrosAmorimDanielBarrrosAmorim BRUniversity ✭✭

    How is the header on Postman?
    Do you any type of Authorization?

    Maybe you could try to remove the Uri and use a String directly on the PostAsync.

    Try this : await client.PostAsync("http://mydomain.com/api/abonados", content);

  • JosecanallaJosecanalla ARMember ✭✭

    Headers in Postman
    Content-Type: application/json
    Accept: application/json

    I try removing the URI and putting directly the string of the URL but get the same error.

  • JosecanallaJosecanalla ARMember ✭✭

    No authorization.

  • JosecanallaJosecanalla ARMember ✭✭
    Accepted Answer

    Well, I found the problem. The Xamarin code is OK, the problem is with the API. I am sending one extra key/value pair in the JSON which is not handled by the API. I don't understand why get 404 error and not 500, but deleting the extra key/value pair I get it working fine.

  • mohdali14mohdali14 Member
    > @Josecanalla said:
    > Well, I found the problem. The Xamarin code is OK, the problem is with the API. I am sending one extra key/value pair in the JSON which is not handled by the API. I don't understand why get 404 error and not 500, but deleting the extra key/value pair I get it working fine.

    John

    I have same issue . Everything works fine . But unable to post data . Getting 404 . Can you tell me where should i delete extra key value pair ?
Sign In or Register to comment.