Reputation: 734
I am receiving a file from my controller as
public async Task<IActionResult> ScanFile(IFormFile request)
and how can I pass this in RestSharp as shown below in AddParameter. I tried setting my own boundary and serialize IFormFile nothing worked out.
var client = new RestClient("https://api_url/requests");
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", "bearer API_TOKEN");
request.AddHeader("accept", "application/json");
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"; filename=\"eicar.com\"\r\nContent-Type: text/plain\r\n\r\nX5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*\r\n-----011000010111000001101001--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Upvotes: 3
Views: 4677
Reputation: 7937
This worked for me.
From my console app,
var client = new RestClient("http://localhost:30000/api/Factory");
var request = new RestRequest("UploadDocumentBlob", Method.POST) { RequestFormat = DataFormat.Json, AlwaysMultipartFormData = true };
request.AddFile("file", Path.Combine(settings.PdfStorePath, "11rwai4q.pdf"));
var uploadDocumentBlobResponse = client.Post<Response>(request).Data;
to my api,
[HttpPost("api/[controller]/[action]")]
public async Task<Response> UploadDocumentBlob(IFormFile file)
the key thing here is that in request.AddFile
the first parameter must be "file"
not any other name.
Hope this helps someone.
Upvotes: 3