How to mount a RestRequest with x-www-form-urlencoded in RestSharp C #

0

I want to make a request using RestSharp when content-type = application/json , I now need to make a request application/x-www-form-urlencoded but I can not find the correct way to do this, here is an example of how I mount the request application/json :

var request = new RestRequest(Method.POST);
request.Resource = "/search";
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json; charset=utf-8");
request.AddParameter("application/json", JsonConvert.SerializeObject(new { bucketId = bucketID, startFileName = fileName, maxFileCount = maxFileCount }), ParameterType.RequestBody);
request.AddHeader("Authorization", authorize_account.authorizationToken);

The way I pass the body when it comes to JSON is this, how do I do when it comes to x-www-form-urlencoded?

    
asked by anonymous 26.07.2018 / 18:47

2 answers

3

It's similar.

You just need to change the header and add each key-value pair with the AddParameter method.

request.AddHeader("content-type", "x-www-form-urlencoded");

//...

request.AddParameter("nome", "valor");
request.AddParameter("1_nome", "1_valor");
    
26.07.2018 / 18:51
0

Another way I found out the @LINQ response is to add the url in the application/x-www-form-urlencoded parameter by typing it manually:

var request = new RestRequest(Method.POST);

request.Resource = "/search";
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept-Version", "v1.0");
request.AddParameter("application/x-www-form-urlencoded", $"api_key={api.api_key}&access_key={api.access_key}&timestamp={api.timestamp}&nonce={api.nonce}&algo=sha1&q=woman&lang=en&page=0&limit=100", ParameterType.RequestBody); 

The difference is readability, I think it's best to use @LINQ's.

    
26.07.2018 / 19:38