简体   繁体   中英

RestSharp doesn't UTF-8 Encode the Request

I'm trying to POST a request with one lonely parameter, as such:

var client = new RestClient("http://www.fluff.com");
var request = new RestRequest("whatever", Method.POST);
request.AddParameter("param", "Оксана");
client.Execute(request);

This results in the following request, notice the bunch of encoded 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 receiver gets those question marks..

How do I make RestSharp properly encode the body as UTF-8, or how do I send the request in a RestSharp friendly way so that she doesn't garble the data?

Christer, that's standard encoding for Content-Type: application/x-www-form-urlencoded , which uses ISO-8859-1 as a default. If you specifically want to tell the server to expect UTF-8, you can add ; charset=UTF-8 ; charset=UTF-8 at the end Content-Type: application/x-www-form-urlencoded ; charset=UTF-8 Content-Type: application/x-www-form-urlencoded ; charset=UTF-8 . But then it's your responsibility to make sure the data you post is really encoded in UTF-8.

Or if you want to do it in "application/json", you can set the content type in this 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 could also use multipart/form-data : <form action="YOUR_ACTION_NAME_HERE" method="post" enctype="multipart/form-data">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM