L
L
LebedevStr2020-08-23 05:03:49
C++ / C#
LebedevStr, 2020-08-23 05:03:49

Am I passing the wrong header in a POST request (C#)?

There is a translator API
https://rapidapi.com/jenio-inc-jenio-inc-default/a...

It has the following example of a POST request to RestSharp (a valid key is nested):

var client = new RestClient("https://kiara-translate.p.rapidapi.com/get_translated/");
var request = new RestRequest(Method.POST);
request.AddHeader("x-rapidapi-host", "kiara-translate.p.rapidapi.com");
request.AddHeader("x-rapidapi-key", "tb0d24d7fffmsh9be6b811c6b83dcp131c79jsn83b8a400788b");
request.AddHeader("content-type", "application/json");
request.AddHeader("accept", "application/json");
request.AddParameter("application/json", "{ "input": "We make the world a better place", "lang": "ja"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);


I have problems with a string, namely with its format:
request.AddParameter("application/json", "{ "input": "We make the world a better place", "lang": "ja"}", ParameterType.RequestBody);

The translation parameter and the text string for translation should be passed here.

Here's the result (using a normal HttpWebRequest):
https://dotnetfiddle.net/iEowhw

Specified value has invalid HTTP Header characters


If not screened
"
produces a syntax error.

How to put a request correctly?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2020-08-23
@LebedevStr

Can't format json by hand, use classes

JsonSerializer.Serialize(new { input = "We make the world a better place" , lang = "ja"})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question