1

I'm making a Web API and I need to post data to another API controller.

The API method POST of the controller looks like this:

public async Task<IActionResult> 
           ImportFile([FromForm] ImportData data)
   {
var result = await _service.ImportData(data);
return Ok(result);
   }

The ImportData looks like this:

public class ImportData
{
    [Required]
    public IFormFile File {get;set;}
    
    [Required]
    public string name{get;set;}
}

I have the same DTO to consume the endpoint of the controller, but for the IFormFile I only have available a encoded64 string, so in my DTO I substitute IFormFile for byte[].

How do I generate the IFormFile with the string to feed the DTO and make the request?

How do I populate the form of the request?(because the controller has [FromForm]

My current request:

ImportDataDTO data = new ImportDataDTO ();
   data.file = Convert.FromBase64String(encodeStringFile);

 data.name = "my name";
 await httpclient.PostAsync(URL, data);

Regards

Relll
  • 11
  • 4
  • please post the way you are requesting it – Daniel A. White Jan 13 '22 at 18:07
  • @DanielA.White there you have it – Relll Jan 13 '22 at 18:15
  • Does this answer your question? [post multipart/form-data in c# HttpClient 4.5](https://stackoverflow.com/questions/49229368/post-multipart-form-data-in-c-sharp-httpclient-4-5) – Jonathan Jan 13 '22 at 18:32
  • `IFormFile` is for non JSON files - its for a normal form upload – Daniel A. White Jan 13 '22 at 19:11
  • @DanielA.White yes that I know. But I'm a bit lost. The `IFormFile` that the controller is specting is a XML. But I Only have the base64 string of the encoded XML file. So I need to send that my model, with the XML(`IFormFile`) and the string name. All of that in `[FromForm]`. Do I need to mock the `IFormFile` first maybe? – Relll Jan 13 '22 at 19:25
  • Could you pls tell us your requirement and what issue you met now? As your description, it seems that your api received a request with a file, and then you want send the file data to another api. So why you need to transfer to string? first but not send the file data directly? I can't understand here. – Tiny Wang Jan 14 '22 at 02:45

0 Answers0