0

Using ReactJS, I have a GET request working with fetch method, and the REST API responds with a few files. How would they be sent to me?

Meaning, can it be parsed with response.json(), so that it can be displayed as actual files from REST API and also be downloaded to local drive?

Here is the method that makes the GET request:

  sendFileRequest() {

      ...

      return dispatch => {
      dispatch({
        type: 'FETCH_API'
      });

      return fetch('http://11.22.33.44:8080/receive/files', getRequest)
      .then(response => response.json()) //Could I do .json() with files being the response?
      .then(APIfiles => {
        //How can I parse the files returned and display them for download to local drive?
      })
  }
4
  • you basically mean the response from ur api is file or json text? Commented Sep 9, 2016 at 4:24
  • You can use JSON.parse(response) , Can you post your api response so that we can get a better idea Commented Sep 9, 2016 at 4:44
  • @MyMasterPeice Will be file Commented Sep 9, 2016 at 6:26
  • @LionelDcosta sorry but typically in what response form is file in? Commented Sep 9, 2016 at 6:27

2 Answers 2

1

fetch({}).then(res => res.json()) or fetch({}).then(res => res.text()), it depend on the content-type of your response.

if your contentType is application/json, you should use res.json(),

if your contentType is text/plain, you should use res.text().

you can check the doc here.

Since your server just return a file, I think you should use res.text().

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

Comments

0

You need to check the likely response content-type with the one who is responsible for developing the API.

What you need to know is that REST is not fastened to JSON. It is all about resources, therefore, you may receive XML, plain text, files, etc.

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.