1

I have a question regarding httpclient, i have a node.js rest api and Im trying to post (send) the user info to the service in order to insert to a database. the service is ok i tested manually and with postman.

But im using vs2017 xamarin and to consume the information im using the httpclient

I convert my user object to a json format

var json = JsonConvert.SerializeObject(user);

result: {"userName":"user","email":"user@hot.com","psw":"jok"}

then I create a content string type and pass the json:

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

then I create the client

var client = new HttpClient();

and i test the following two codes

if i use this code the service work and the data is inserted in the data base but i think is because im like manually passing the parameters

HttpResponseMessage response = await client.PostAsync("http://localhost/ws/postUser/"+ e.userName + "/" + e.email + "/" + e.psw, content);

but what i was expecting is that this code works but in the server im getting the error that the url is not find. I think i need to map the parameters with the content

HttpResponseMessage response = await client.PostAsync("http://localhost/ws/postUser/", content);

The URL of the service is

http://localhost/ws/postUser/:userName/:email/:psw

this is the complete code: I have a OnSignUpEventArgs class that inherit from EventArgs and where i declare the user object.

 private async void SigUpDialog_mOnSigUpComplete(object sender, OnSignUpEventArgs e)
    {




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

        var client = new HttpClient();
       HttpResponseMessage response = await client.PostAsync("http://localhost/ws/postGasUser/"+ e.userName + "/" + e.email + "/" + e.psw, content);

      //  HttpResponseMessage response = await client.PostAsync("http://localhost/ws/postGasUser/", content);


        if (response.IsSuccessStatusCode)
        {
            Console.Write("Success");

        }
        else
        {
            Console.Write("Error");

        }




    }
  • do NOT use localhost. That refers to the mobile device (or emulator), which is not where your service is running. Use the IP or FQDN instead. – Jason Jul 25 '17 at 22:58
  • 1
    try to use the server IP address – Ganesh Cauda Jul 26 '17 at 05:10
  • Maybe I'm not understanding but are you saying that the URL of the service is `http://localhost/ws/postUser/:userName/:email/:psw` and posting to `http://localhost/ws/postUser/` returns 404? Seems correct to me - it's not the URL of the service! – Todd Menier Aug 01 '17 at 14:41
  • Thanks it work with the IP address – Betin Beton Aug 24 '17 at 15:37

0 Answers0