如何通过使用RestSharp推动ND-JSON文件来提出发布请求?

发布于 2025-02-08 17:49:20 字数 1525 浏览 2 评论 0原文

我正在尝试获取以下curl命令的restSharp(v107)等效:

curl --insecure -H "Content-Type: application/x-ndjson" -XPOST -u user:password https://localhost:9200/products/_bulk --data-binary "@pathtofile/products-bulk.json"

文件:products-bulk.json,包含许多由Endlines(NDJSON格式)隔开的JSON对象。

这是我尝试这样做的尝试:

string base_url = "https://localhost:9200";

// disabling SSL certificate (for my specific use, you could ignore that)
var options = new RestClientOptions(base_url)
{
    RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
};

var client = new RestClient(options);
string username = "user";
string password = "password";
client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest() ;
request.Method = Method.Post;
request.Resource = "products/_bulk";
request.RequestFormat = DataFormat.Binary; // sending via --data-binary format
request.AddHeader("Content-Type", "application/ndjson");

string path = @"pathtofile\products-bulk.json";
request.AddFile("file",path);
var response = client.Execute(request);

但是,NDJSON似乎不是支持格式,并引发以下错误:

{“ error”:“ content-type标头[application/ndjson; boundard =“ 03713f22-b45c-40ed-a9f6-50949a99a017”]不支持”,“状态”,“状态”:406}

一些在线消息来源说,有些在线消息来源说使用了 json 而不是request.Addheader中的 ndjson ,并通过RestClientOptions作为解决方法添加ISO-8859-1。但这对我不起作用,给我带来了不同的错误。 (目前,我无法提取来源。如果发现它,我会进行更新)

。我是编程的新手。对于所使用的任何不正确的术语表示歉意。

I'm trying to get the RestSharp (v107) equivalent of the following cURL command:

curl --insecure -H "Content-Type: application/x-ndjson" -XPOST -u user:password https://localhost:9200/products/_bulk --data-binary "@pathtofile/products-bulk.json"

The file: products-bulk.json, contains many json objects separated by endlines (ndjson format).

Here is my attempt at doing this:

string base_url = "https://localhost:9200";

// disabling SSL certificate (for my specific use, you could ignore that)
var options = new RestClientOptions(base_url)
{
    RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true
};

var client = new RestClient(options);
string username = "user";
string password = "password";
client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest() ;
request.Method = Method.Post;
request.Resource = "products/_bulk";
request.RequestFormat = DataFormat.Binary; // sending via --data-binary format
request.AddHeader("Content-Type", "application/ndjson");

string path = @"pathtofile\products-bulk.json";
request.AddFile("file",path);
var response = client.Execute(request);

However, ndjson seems to be not a supported format and throws the following error:

{"error":"Content-Type header [application/ndjson; boundary="03713f22-b45c-40ed-a9f6-50949a99a017"] is not supported","status":406}

Some online source said to use json instead of ndjson in request.AddHeader and add an iso-8859-1 encoding through RestClientOptions as a workaround. But that didn't work for me and throws me a different error. (At the moment, I'm unable to pull the source. I'll update it if I find it)

Any help is appreciated. I'm fairly new to programming. Apologies for any incorrect terminologies used.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文