RestSharp does not encode UTF-8 request
I am trying to send a request with one lonely parameter:
var client = new RestClient("http://www.fluff.com");
var request = new RestRequest("whatever", Method.POST);
request.AddParameter("param", "");
client.Execute(request);
This leads to the following request: notice the bunch of coded question marks:
POST http://www.fluff.com/whatever HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp/105.0.1.0
Content-Type: application/x-www-form-urlencoded
Host: www.fluff.com
Content-Length: 24
Accept-Encoding: gzip, deflate
param=%3F%3F%3F%3F%3F%3F
Imagine the sadness when the recipient receives these question marks.
How do I get RestSharp to properly encode the body as UTF-8, or how do I send the request in a RestSharp-friendly way so that it doesn't garble the data?
source to share
Christer, this is the default encoding Content-Type: application/x-www-form-urlencoded
that uses ISO-8859-1 by default. If you specifically want to tell the server to expect UTF-8, you can add ; charset=UTF-8
at the end Content-Type: application/x-www-form-urlencoded ; charset=UTF-8
. But then it is your responsibility to ensure that the data you post is indeed UTF-8 encoded.
Or if you want to do it in "application / json" you can set the content type that way (I got this from http://itanex.blogspot.com/2012/02/restsharp-and-advanced-post-requests.html ):
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
You can also use multipart/form-data
:<form action="YOUR_ACTION_NAME_HERE" method="post" enctype="multipart/form-data">
source to share