0

I am trying to use an API and the example they have given is in the form of a Curl command:

curl --location --request POST 'https://dev-api.itranslate.com/translation/v2/' --header 'Authorization: Bearer 603160b7-cee1-4c13-bcd7-37420b55211d' --header 'Content-Type: application/json' --data-raw '{
    "source": {"dialect": "en", "text": "Hello World"},
    "target": {"dialect": "es"}
}'

I am trying to replicate this using RestSharp. However, in all the examples I can find for RestSharp the parameters are in neat name-value pairs. But in this case the parameters are different, the first one is called "source" and it consists of two further name-value pairs.

I have tried syntax like this:

request.AddHeader("Authorization", "Bearer 603160b7-cee1-4c13-bcd7-37420b55211d");

request.AddParameter("source", "dialect:'en'");

request.AddParameter("source", "Text:'Hello World'");

request.AddParameter("target", "dialect:'es'");

But the server doesn't respond, I assume because it doesn't understand the request. How can I shoehorn these three things ("Source", "Dialect" and "en") into one name-value pair?

1 Answers1

0

Your curl request posts a JSON object. Your code, however, posts a URL encoded form.

public record RequestPayload(Source Source, Target Target);
public record Source(string Dialect, string Text);
public record Tagret(string Dialect);


var client = new RestClient("https://dev-api.itranslate.com/translation/v2/");
client.UseAuthenticator(new JwtAuthenticator("603160b7-cee1-4c13-bcd7-37420b55211d"));
var payload = new RequestPayload(new Source("en", "Hello world), new Target("es));
var request = new RestRequest().AddJsonBody(payload);
var response = await client.ExecutePostAsync(request);
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83