0

Our web application is developed using React and Spring Boot. We have an existing feature to download the data in an Excel file.

Following piece of code is used to send a GET request to generate and download the document.

window.location.assign(exportRESTEndPoint);

But now API developers want to change the end point from GET to POST to support more data, but window.location.assign does not support a POST request.

Any suggestions on how to download a file from server which requires a POST request with 'Content-Type': 'application/x-www-form-urlencoded'

Thulasi_G
  • 126
  • 1
  • 14
  • Create a form and submit it? – ray hatfield Sep 11 '20 at 21:26
  • Hi @rayhatfield, but how come the form submission will download the generated file from server? – Thulasi_G Sep 11 '20 at 21:32
  • A form submission allows you to specify the method (POST) and the server is presumably either sending a [content-disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) header indicating that it should be downloaded ("attachment") and/or it's sending a mimetype that the browser will download anyway. You could send a POST using the [fetch api](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) but it's going to be more complicated than simply submitting a form. – ray hatfield Sep 11 '20 at 21:55
  • I'm making assumptions here based on the fact that you were previously downloading it by simply navigating to an endpoint. – ray hatfield Sep 11 '20 at 22:04
  • Great! As you mentioned the form post worked to send request and download the excel file. For anyone having questions on how to send the request I followed this https://stackoverflow.com/a/133997/1423854 – Thulasi_G Sep 15 '20 at 22:03

0 Answers0