Shahabuddin Vansiwala
Shahabuddin Vansiwala

Reputation: 673

How to send parameter in REST api in xamarin.forms?

How to send parameter in REST api in xamarin.forms ?

I have created REST API Project in xamarin using PCL.

When I call Simple REST api using Below code in Xamarin.forms (Portable Class Library) then I have Successfully receive json Response.

using (var cl = new HttpClient())
{
    var result = await cl.GetStringAsync("http://192.168.1.100/apps/jara/web/api/user/test");
    jsonResponseClass des = JsonConvert.DeserializeObject<jsonResponseClass>(result);
    lbl1.Text = des.code + " " + des.status + " " + des.message;
}

public class jsonResponseClass
{
    public string code { get; set; }
    public string status { get; set; }
    public string message { get; set; }
}

Using above code I got a response

{
    code: 200,
    status: "ok",
    message: "hello"
}

REST API response type is JSON and type is POST

Now, I want to call below Login Api using paramater.

http://192.168.1.100/apps/jara/web/api/user/login

Paramater : email_id and Password

this api success response type is....

{
    code: 200,
    status: "Success",
    User: {
        userid: 126,
        token: "d154s4d54s654df5s4df56s4df564s5df4",
        email: "[email protected]",
        mobile_number: "9898989898"
    },
    message: "Successfully logged in"
}

What can i do ?

Upvotes: 1

Views: 2632

Answers (1)

Shahabuddin Vansiwala
Shahabuddin Vansiwala

Reputation: 673

Finally i can do this using below code....

using (var cl = new HttpClient())
{
    var formcontent = new FormUrlEncodedContent(new[]
    {
            new KeyValuePair<string,string>("email_id","[email protected]"),
            new KeyValuePair<string, string>("password","shah")
        });


    var request = await cl.PostAsync("http://192.168.1.100/apps/jara/web/api/user/login", formcontent);

    request.EnsureSuccessStatusCode();

    var response = await request.Content.ReadAsStringAsync();

    jsonResponselogin res = JsonConvert.DeserializeObject<jsonResponselogin>(response);

    lbl1.Text = res.code + " " + res.status + " " + res.message;

}

This code helpful for Calling REST API with Parameter

Thank you...

Upvotes: 2

Related Questions