简体   繁体   中英

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

cURL example that's provided:

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.

My code that's trying to accomplish the same thing as above cURL is basically this:

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.

Update Feb 20th: I still cannot figure this out. In the meantime since I need a solution I'm actually calling the curl executable to accomplish this. Naturally, I'd be extremely grateful if someone had any insight in figuring this out through RestSharp instead.

the cUrl idicates this to be a multipart/form request. see the following for a bit of detail

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. 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. My original code generated a parameter called like the file name (as per my AddFile call) which was causing the error. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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