Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing list of int to Web API from RestSharp Client

When I use code below from REST Sharp I am not able to pass listOfSelectedTicketsIds it always null.

Request of Rest Sharp .net Client

 var _stDeveloperApi = new RestClient("http://127.0.0.1/");
            var url = string.Format("api/v1/SignalR/MultiClickMode");
            var listOfSelectedTicketsIds = new List<int> { 2, 3 };
            var request = new RestRequest(url, Method.GET);
            request.AddParameter("listOfSelectedTicketsIds", listOfSelectedTicketsIds, ParameterType.GetOrPost);
            var response = _stDeveloperApi.Execute(request);

Web API Method

[HttpGet]
        public HttpResponseMessage MultiClickMode(List<int> listOfSelectedTicketsIds)
        { 
            var response = new HttpResponseMessage();

            try
            {

            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            return response;
        }
like image 494
SOF User Avatar asked Jan 21 '14 23:01

SOF User


People also ask

Which is better RestSharp or HTTP client?

The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.

How do you add parameters in RestSharp?

var client = new RestClient("http://localhost"); var request = new RestRequest("resource", Method. POST); request. AddParameter("auth_token", "1234"); request. AddBody(json); var response = client.

What is RestSharp Netcore?

NET project. RestSharp is an open-source HTTP Client library that we can use to consume APIs easily. RestSharp features automatic serialization and deserialization, request and response type identification, and numerous authentication inbuilt patterns.


1 Answers

Change your client code like this.

var _stDeveloperApi = new RestClient("http://127.0.0.1/");
var url = string.Format("api/v1/SignalR/MultiClickMode");
var listOfSelectedTicketsIds = new List<int> { 2, 3 };
var request = new RestRequest(url, Method.GET);
listOfSelectedTicketsIds.ForEach(t => 
            request.AddParameter(
                "listOfSelectedTicketsIds", t, ParameterType.GetOrPost));
var response = _stDeveloperApi.Execute(request);

Change the web API action method signature like this.

public HttpResponseMessage MultiClickMode(
                               [FromUri]List<int> listOfSelectedTicketsIds)
like image 167
Badri Avatar answered Oct 21 '22 09:10

Badri