Set up headers in Axios?

Asked

Viewed 749 times

0

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

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

headers of the Postman:

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 didn’t understand).

If anyone can help me, I’d appreciate it...

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)
      })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.