Configure headers on Axios?

0

I'm using Axios to make the requests between client and server, but I'm having problems with headers . When I do the requests Postman returns exactly what I want (which in this case is a PDF), but when I make the request through my client it returns a PDF that is not what I want, I do not even know where it is from that PDF is coming.

I used Beyond Compare and I could see the differences in headers , and I'll show below:

Postman headers:

headers: { host: 'localhost:9000', connection: 'keep-alive', 'postman-token': '75a0b30b-03ae-574f-fa40-d36c30e14e76', 'cache-control': 'no-cache', encoding: 'UTF-8', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'content-type': 'application/pdf', accept: '*/*', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4' }, rawHeaders: [ 'Host', 'localhost:9000', 'Connection', 'keep-alive', 'Postman-Token', '75a0b30b-03ae-574f-fa40-d36c30e14e76', 'Cache-Control', 'no-cache', 'Encoding', 'UTF-8', 'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'Content-Type', 'application/pdf', 'Accept', '*/*', 'Accept-Encoding', 'gzip, deflate, br', 'Accept-Language', 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4' ]

Customer Headers:

headers: { cookie: 'Phpstorm-ede8a916=ef6f3db8-812c-47ae-84e1-fcd03915f7f4', 'accept-language': 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4', 'accept-encoding': 'gzip, deflate, br', referer: 'http://localhost:8080/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', accept: 'application/json, text/plain, */*', 'cache-control': 'no-cache', pragma: 'no-cache', connection: 'close', host: 'localhost:9000' }, rawHeaders: [ 'cookie', 'Phpstorm-ede8a916=ef6f3db8-812c-47ae-84e1-fcd03915f7f4', 'accept-language', 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4', 'accept-encoding', 'gzip, deflate, br', 'referer', 'http://localhost:8080/', 'user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'accept', 'application/json, text/plain, */*', 'cache-control', 'no-cache', 'pragma', 'no-cache', 'connection', 'close', 'host', 'localhost:9000' ]

axios.get('/server/baixarpdf', { headers: {'content-type': 'application/pdf'}})
      .then(function(response) {
        let blob = new Blob([response.data], {
          type: 'application/pdf'
        })
        let url = window.URL.createObjectURL(blob)
        window.open(url);
      })
      .catch(function(error) {
        console.log(error)
      })

The differences are in 'content-type' and 'accept', and I want to know how to change these settings, because I could not find what I need in the Axios documentation (or I did not understand).

If someone can help me, thank you ...

    
asked by anonymous 04.08.2017 / 21:26

1 answer

0

I found another solution, follow the code:

axios.get('/server/arquiv', {responseType: 'arraybuffer'})
      .then(function(response) {

        let file = new Blob([response.data], {type: 'application/pdf'});
        let fileURL = URL.createObjectURL(file);
        window.open(fileURL);
      })
      .catch(function(error) {
        console.log(error)
      })
    
04.08.2017 / 22:10