如何通过使用RestSharp推动ND-JSON文件来提出发布请求?
我正在尝试获取以下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论