繁体   English   中英

将cURL -F移植到RestSharp中

[英]Porting cURL -F into RestSharp

我正在尝试使用第三方API(实际上是Zendesk)上传本地文件,请有人帮我了解如何将提供的示例cURL移植到RestSharp中

提供的cURL示例:

curl https://{subdomain}.zendesk.com/api/v2/help_center/articles/{id}/attachments.json \ -F "inline=true" -F "file=@drawing.png" \ -v -u {email_address}:{password} -X POST

我一直在尝试使用AddFile将它添加为参数,但是我一直遇到500 Internal Server Error。

我的代码试图完成与上述cURL相同的操作,基本上是这样的:

var attachmentRequest = new RestRequest();

var attachmentURL = "api/v2/help_center/articles/" + article.id + "/attachments.json";
var attachmentPath = articlePath + "\\" + attachment.file_name;

attachmentRequest.Resource = attachmentURL;
attachmentRequest.Method = Method.POST;
attachmentRequest.AddParameter("inline", attachment.inline);
attachmentRequest.AddFile(attachment.file_name, attachmentPath);

var attachmentResponse = client.Execute(attachmentRequest); // returns status code 500

非常感谢您的帮助,我搜索了很多有关移植这些请求的信息,以及cURL和RestSharp文档,但我根本无法弄清楚。

2月20日更新:我仍然无法弄清楚。 同时,由于我需要一个解决方案,因此实际上我在调用curl可执行文件来完成此任务。 自然,如果有人通过RestSharp解决此问题,我将不胜感激。

cUrl将此表示为多部分/表单请求。 请参阅以下详细信息

https://blog.tyrsius.com/restsharp-file-upload/

我相信您要使用的代码行是

request.AlwaysMultipartFormData = true;

我现在在摆弄类似的要求,因此我的结果不正确。

我能够与供应商(Zendesk)联系,并查看此请求的情况。 事实证明,我要做的就是确保有效负载符合cURL的规范,并使用参数名“ file”而不是文件名。 我的原始代码生成了一个称为文件名的参数(根据我的AddFile调用),该参数导致了错误。 如此简单:

attachmentRequest.AddFile("file", attachmentPath);

显然,服务器需要一个名为“ file”的参数,并且能够从文件元数据中成功读取文件名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM