How to assemble a Restrequest with x-www-form-urlencoded in Restsharp C#

Asked

Viewed 3,129 times

2

I know how to requisition using Restsharp when the content-type = application/json, now I need to make a request application/x-www-form-urlencoded but I can’t find the right way to do it, follow the one example of how I mount the requestapplication/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, as I do when it comes to x-www-form-urlencoded?

2 answers

5


Is similar.

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

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

//...

request.AddParameter("nome", "valor");
request.AddParameter("1_nome", "1_valor");
  • It worked ! Thanks, I actually managed to figure out another way to do it, but yours is more readable.

  • It worked, I just had to change the contenttype to this: request.Addheader("content-type", "application/x-www-form-urlencoded");

0

Another way that I discovered out of the @LINQ response is to add the url in the parameter application/x-www-form-urlencoded writing 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 the readability, I thought better to use the @LINQ same.

  • It is because this is the end result of the LINQ approach

  • Yep :P anyway wanted to post because it’s another solution right ?

Browser other questions tagged

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