M
M
Maxim2018-10-13 12:28:21
JSON
Maxim, 2018-10-13 12:28:21

How to read stream+json responses from Vuejs?

I use (for now) axios and here is the code:

axios.get('http://localhost:8081/pets')
        .then(response => {
          this.pets = response.data;
        })

If you return a regular application/json from the server, then everything is fine.
Now I want to read application/stream+json as values ​​arrive, i.e. something like this (but it doesn't work like this):
axios.get('http://localhost:8081/pets')
        .then(response => {
          this.pets.push(response.data)
        })

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2018-10-16
@mgramin

It looks like what I want to do is called SSE:

let es = new EventSource('http://localhost:8081/pets');
es.addEventListener('message', event => {
    let data = JSON.parse(event.data);
    this.pets.push(data);
}, false);

N
nakree, 2018-10-13
@nakree

Example from axios github

axios({
  method:'get',
  url:'http://bit.ly/2mTM3nY',
  responseType:'stream'
})
  .then(function (response) {
    response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question