Matthew Wherry
Matthew Wherry

Reputation: 353

Issues with Uploading a file with request

My API method

[HttpPut($"Create")]
public async Task<ActionResult<MyResult>> CreateAttachment([FromBody]MyRequest request, [FromForm]IFormFile file)

and my RestSharp call

Client.Put<MyResult>(new RestRequest($"{Resource}/Create", Method.Put)   
      .AddJsonBody(request)
      .AddFile("file", fileData, request.FileName)
      .AddHeader("Content-Type", "multipart/form-data"))!;

My restsharp API keeps throwing an UnsupportedMediaType exception, and when i try to debug the call in a middleware logger in the API, my debugger and app die with a stack overflow exception...I've tried specifying the content type in the call but that also fails.

Trying to write a put call that accepts both a file and also a JSON object with info on the file.

Upvotes: 0

Views: 458

Answers (1)

mazhar zarsaw
mazhar zarsaw

Reputation: 146

Make sure that you're specifying the Content-Type header before adding the file and JSON body to the request, and that your API method is expecting a multipart/form-data request. If you continue to have issues, you may want to try using a tool like Fiddler to inspect the request and see if there are any issues with the request data.

Upvotes: 1

Related Questions