Search code examples
c#jsonxamarinurlget

How to get data in Xamarin C# url with body form-data


Hy Guys, I'm trying to download data from a url by logging in with a php Web Service but I don't know how to do it. If I make a POST request via postman inserting in BODY -> form-data the email and password, it gives me this:

{"Users": [{"email": "[email protected]", "nickname": "nickname", "image": "http: \ / \ / localhost \ / MyWebService \ / images \ /test_img.png "}]}

So I created a class in cs like this:

public class Users
    {
        [JsonProperty("users", NullValueHandling = NullValueHandling.Ignore)]
        public User[] UsersUsers { get; set; }
    }

    public class User
    {
        [JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)]
        public string email { get; set; }

        [JsonProperty("nickname", NullValueHandling = NullValueHandling.Ignore)]
        public string nickname { get; set; }

        [JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)]
        public string password { get; set; }

        [JsonProperty("image", NullValueHandling = NullValueHandling.Ignore)]
        public Uri image { get; set; }
    }

Instead in the botton function I tried to write this code to contact the Service and make the call :

async private void login(Object sender, EventArgs e)
        {
            string email = lbl_email.Text;
            string password = lbl_password.Text;
            string url = "http://192.168.178.77/TestLoginURL/api/login.php";


            var formContent = new FormUrlEncodedContent(new[]
            {
              new KeyValuePair<string, string>("email", email),
              new KeyValuePair<string, string>("password", password),
            });

            string contentType = "application/json";
            JObject json = new JObject
            {
                { "email", email},
                { "password", password }
            };
            HttpClient client = new HttpClient();
            var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));
            var data = await response.Content.ReadAsStringAsync();
            var users = JsonConvert.DeserializeObject<Users>(data);

        }

Obviously it is crushed and the error that gives me back is this: Newtonsoft.Json.JsonReaderException Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

So, someone can help me? Where am I wrong? Thanks


Solution

  • Solved in this way :

    async private void login(Object sender, EventArgs e)
    {
        string email = lbl_email.Text;
        string password = lbl_password.Text;
        string url = "http://192.168.178.77/TestLoginURL/api/login.php";
    
        Users users = new Users();
    
        FormUrlEncodedContent formContent = new FormUrlEncodedContent(new[]
        {
          new KeyValuePair<string, string>("email", email),
          new KeyValuePair<string, string>("password", password),
        });
    
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "application/json");
    
        try
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            var responseMessage = client.PostAsync(url, formContent).Result;
            var data = await responseMessage.Content.ReadAsStringAsync();
            users = JsonConvert.DeserializeObject<Users>(data);
            lbl_nickname.Text = users.UsersUsers[0].nickname;
    
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
            throw;
        }
    }
    

    The way in which I passed the parameters to the url was practically wrong and consequently it gave me error by not recognizing the email and password I sent.