简体   繁体   English

将cURL -F移植到RestSharp中

[英]Porting cURL -F into RestSharp

I'm trying to upload local file(s) using the 3rd party API (actually Zendesk), could someone please help me understand how to port the example cURL that was provided into RestSharp 我正在尝试使用第三方API(实际上是Zendesk)上传本地文件,请有人帮我了解如何将提供的示例cURL移植到RestSharp中

cURL example that's provided: 提供的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

I've been trying to add it with AddFile, also as parameter, but I keep running into 500 Internal Server Error. 我一直在尝试使用AddFile将它添加为参数,但是我一直遇到500 Internal Server Error。

My code that's trying to accomplish the same thing as above cURL is basically this: 我的代码试图完成与上述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

Any help is greatly appreciated, I searched through a lot of information about porting these requests, as well as both cURL and RestSharp documentation and I simply cannot figure this one out. 非常感谢您的帮助,我搜索了很多有关移植这些请求的信息,以及cURL和RestSharp文档,但我根本无法弄清楚。

Update Feb 20th: I still cannot figure this out. 2月20日更新:我仍然无法弄清楚。 In the meantime since I need a solution I'm actually calling the curl executable to accomplish this. 同时,由于我需要一个解决方案,因此实际上我在调用curl可执行文件来完成此任务。 Naturally, I'd be extremely grateful if someone had any insight in figuring this out through RestSharp instead. 自然,如果有人通过RestSharp解决此问题,我将不胜感激。

the cUrl idicates this to be a multipart/form request. cUrl将此表示为多部分/表单请求。 see the following for a bit of detail 请参阅以下详细信息

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

i believe the line of code you are looking to use is 我相信您要使用的代码行是

request.AlwaysMultipartFormData = true;

Im fiddling with a similar request right now, so ill post with my results. 我现在在摆弄类似的要求,因此我的结果不正确。

I was able to get in touch with the vendor (Zendesk) and review what is going on with this request. 我能够与供应商(Zendesk)联系,并查看此请求的情况。 Turns out that all I needed to do was make sure that the payload matches the specification as per the cURL, with parameter name "file" as opposed to file name. 事实证明,我要做的就是确保有效负载符合cURL的规范,并使用参数名“ file”而不是文件名。 My original code generated a parameter called like the file name (as per my AddFile call) which was causing the error. 我的原始代码生成了一个称为文件名的参数(根据我的AddFile调用),该参数导致了错误。 So it was as simple as this: 如此简单:

attachmentRequest.AddFile("file", attachmentPath);

Apparently the server expects a parameter called "file" and is able to successfully read the filename from the file metadata. 显然,服务器需要一个名为“ file”的参数,并且能够从文件元数据中成功读取文件名。

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

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