Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use PUT in RestSharp?

Tags:

c#

restsharp

I want to use PUT, but I can only find examples of how to use POST. The json data I want to send is sent using this cURL command curl -i -H "Content-Type: application/json" -X PUT -d {"status":1}'http://192.168.0.3:1337/auto/api/v1.0/relays/3 Also I want the "1" after status and the last "3" to be variables.

like image 899
Inane Avatar asked Feb 06 '16 21:02

Inane


People also ask

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.

How do I send a post request on RestSharp?

It's better to specify the Json data body. var client = new RestClient("URL"); var request = new RestRequest("Resources",Method. POST); request. RequestFormat = RestSharp.

What is the use of RestSharp?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.


1 Answers

Set the method as you create the rest request:

public void Update(int id, Product product)
{
  var request = new RestRequest("Products/" + id, Method.PUT);
  request.AddJsonBody(product);
  client.Execute(request);
}

(Source)


(Aircode Warning)

  var status = 1;
  var id = 3;
  var request = new RestRequest("/auto/api/v1.0/relays/" + id, Method.PUT);
  request.AddJsonBody(new { status = status });
  client.Execute(request);

(Compiling Fiddle)

like image 53
NikolaiDante Avatar answered Nov 13 '22 14:11

NikolaiDante