c# how to use the POST method with Restsharp

Asked

Viewed 3,878 times

1

I have a code to log in, but when I send the correct data the API continues to send the code "Unauthorized".

The code is as follows::

var client = new RestClient("link");
var request = new RestRequest("/v1.5/auth/authenticate", Method.POST);
request.RequestFormat = DataFormat.Json;
string user = "{\"nif\":\"15551\", \"nome\":\"teste\", \"password\":\"teste2\", \"loja\": 17 }";
request.AddParameter("text/json", user, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);
  • Taking a look at the Restsharp documentation, shouldn’t you use the Addbody() method to add your json? This Addparameter is a little weird with this "text/json".

1 answer

2


My problem was not having the "user" before the beginning of the JSON code and the Addparameter was wrong.

var client = new RestClient("link");
var request = new RestRequest("/v1.5/auth/authenticate", Method.POST);
request.RequestFormat = DataFormat.Json;
string user = "{\"user\":{\"nif\":\"15551\", \"nome\":\"teste\", \"password\":\"teste2\", \"loja\": 17 }}";
request.AddParameter("application/json; charset=utf-8", user, ParameterType.RequestBody);

IRestResponse response = client.Execute(request);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.