1

I'm trying to do a POST request with multipart/form-data from an Angular 6 application to a REST service in ASP.NET Core, but I have a Error 500. I tried too many solutions, but nothing worked. I tried to execute the request on Postman and get the same problem, but I tried in other software called ARC (Advanced REST Client) and works like a charm and I don't have any idea why.

On the server-side, I getting InvalidDataException: Missing content-type boundary. In my project, I'm using swagger too

here is my code

The request in angular:

public uploadPlanilha(planilha: File, idLote: number): Observable<Array<RequisicaoComposicao>>{
    let formData = new FormData();

    formData.append('arquivo', planilha, planilha.name);
    formData.append('idLote', idLote.toString());

    let httpHeaders = new HttpHeaders();
    httpHeaders = httpHeaders.set("Content-Type", "multipart/form-data");

    return this.httpClient.post<Array<RequisicaoComposicao>>(`${this.API_URL}requisicoes/upload`, formData, {
      headers: httpHeaders
    });
  }

The controller method in Web Api

[HttpPost]
        [Route("upload")]
        [Consumes("multipart/form-data")]
        [DisableRequestSizeLimit]
        public ActionResult<List<RequisicaoComposicao>> PostPlanilhaRequisicoes([FromForm]ArquivoDto arquivoDto)
        {
            try
            {
                using (Stream arquivoPlanilha = arquivoDto.Arquivo.OpenReadStream())
                {
                    List<RequisicaoComposicao> requisicaoComposicoes = _composicaoGestor.Inserir(arquivoPlanilha, int.Parse(arquivoDto.IdLote));
                    return Ok(requisicaoComposicoes);
                }
            }
            catch (Exception)
            {
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }

The ArquivoDto class

public class ArquivoDto
        {
            public IFormFile Arquivo { get; set; }
            public string IdLote { get; set; }
        }
4
  • HttpHeaders is immutable, you need to set the header like let httpHeaders = new HttpHeaders().set("Content-Type", "multipart/form-data").set("Accept", "multipart/form-data"); otherwise each time you call set a new instance of HttpHeaders is returned and your previous header is overwritten . That's why you are not getting the content-type header at the server. Commented Mar 28, 2019 at 14:07
  • I changed this, but still not working. I tried with Content-Type on header and tried without this parameter too, but anything worked. Commented Mar 28, 2019 at 14:11
  • try to use it like let httpHeaders = new HttpHeaders().set("Content-Type", "multipart/form-data") , you can't re-assign the httpHeaders variable. Commented Mar 28, 2019 at 14:13
  • Also if you are using [FromForm] then the Content-Type should be application/x-www-url-formencoded , but if you only want to post the file then remove [FromForm] and use (IFormFile arquivoDto) as the parameter, the content-type should be multipart/form-data in this case Commented Mar 28, 2019 at 14:22

2 Answers 2

1

Remove the [FromForm] tag you don't need it.

On this line here: formData.append('arquivo', planilha, planilha.name);

change it to formData.append('arquivo', planilha);

I never used it, therefore I don't think you need the [Consumes("multipart/form-data")] attribute ether. (unless you are using something like swagger and you want to tell it what this method is consuming then keep it)

I'd also remove this line httpHeaders = httpHeaders.set("Accept", "multipart/form-data");

Sign up to request clarification or add additional context in comments.

4 Comments

I'm using swagger. I removed the codes that you suggested, but still not working.
try to remove the httpHeaders = httpHeaders.set("Content-Type", "multipart/form-data"); line as well.
I tried this too. Now, I get error 500. In the backend, the error is: System.IO.InvalidDataException: Missing content-type boundary
@ViníciusdaCruzMaia check my comment above
0

I use AngularJS + ASP.NET Core 2.1 and find the same error --> System.IO.InvalidDataException: Missing content-type boundary.

My solution is setting 'Content-Type: undefined' in http request front-end. I read somewhere at StackOverflow, you may give it a quick try if it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.