如何将此三部分卷曲命令转换为RestSharp?

发布于 2025-02-03 09:44:18 字数 804 浏览 2 评论 0原文

我正在尝试使用API​​,而他们给出的示例是以卷曲命令的形式:

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"}
}'

我试图使用RestSharp复制它。但是,在所有示例中,我可以找到restsharp的参数以整洁的名称值对。但是在这种情况下,参数是不同的,第一个被称为“源”,它由另外两个名称值对组成。

我尝试过这样的语法:

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'");

但是服务器没有响应,我认为因为它不了解请求。我如何将这三件事(“源”,“方言”和“ en”)分解为一个名称值对?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2025-02-10 09:44:18

您的curl请求帖子json对象。但是,您的代码发布了URL编码表格。

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文