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: "shahbuddin@gmail.com",
mobile_number: "9898989898"
},

message: "Successfully logged in"
}

What can i do ?

Answers

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

    using (var cl = new HttpClient())
    {
        var formcontent = new FormUrlEncodedContent(new[]
        {
                new KeyValuePair<string,string>("email_id","shahbuddin@peerbits.com"),
                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...

  • AMD21AMD21 USMember

    Thanks, it's working for me.

  • AmayindiLynnAmayindiLynn USMember ✭✭

    @Shahabuddin Vansiwala please can I get the full project, am having issues with mine

  • Umar3xUmar3x FRMember ✭✭

    @AmayindiLynn

    What issue have you got ?

    To pass parameter in a simple manner (IMO) I just use a simple DTO with fields I want to send as parameter, serialize it and done.
    Don't forget the JsonProperties. Exemple with a login DTO.

       public class ClassicLoginDto
        {
            [JsonProperty("username")]
            public string username { get; set; }
            [JsonProperty("password")]
            public string password { get; set; }
        }
    
  • AmayindiLynnAmayindiLynn USMember ✭✭

    @Umar3x An error occurred while sending the request ---> System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in :0
    at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0016d] in :0

  • AmayindiLynnAmayindiLynn USMember ✭✭

    @Umar3x
    1. An error occurred while sending the request ---> System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
    2. at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in :0
    3. at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0016d] in :0

  • AmayindiLynnAmayindiLynn USMember ✭✭

    @Umar3x I am receiving a:
    ERROR An error occurred while sending the request
    System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in :0
    at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0016d] in :0

  • AmayindiLynnAmayindiLynn USMember ✭✭

    @Umar3x I am receiving a:
    ERROR An error occurred while sending the request
    System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in :0
    at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0016d] in :0

  • AmayindiLynnAmayindiLynn USMember ✭✭

    @Umar3x I am receiving a:
    ERROR An error occurred while sending the request
    System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in :0
    at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0016d] in :0

  • AmayindiLynnAmayindiLynn USMember ✭✭

    @Umar3x
    ERROR An error occurred while sending the request
    System.Net.Http.HttpRequestException: An error occurred while sending the request ---> System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000b6] in :0
    at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0016d] in :0

Sign In or Register to comment.