簡體   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