What am I doing wrong? #782
-
Hi folks, I'm trying to use undici with the GitHub api but I'm hitting something strange and struggling to debug it effectively. Is it something to do with https? I'm running the following script with node 16: 'use strict'
import undici from 'undici'
import http from 'http'
undici.request(
'https://api.github.com/zen',
(error, { statusCode, headers, body }) => {
if (error) {
console.error(error)
process.exit(1)
}
console.log(`Staus: ${statusCode}`)
console.log(`Status Message: ${http.STATUS_CODES[statusCode]}`)
console.log('Headers', headers)
const bufs = []
body.on('data', buf => { bufs.push(buf) })
body.on('end', () => {
console.log(Buffer.concat(bufs).toString('utf-8'))
})
}
) This is the Hello World example for the github rest api |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 13 replies
-
Heres the same endpoint with curl:
|
Beta Was this translation helpful? Give feedback.
-
Btw, here is the output of your script:
|
Beta Was this translation helpful? Give feedback.
-
I get the same problem with the node core client. I don't think this is strictly problem with undici. I recommend opening an issue on the Node core repo. If we find any problem there I'm sure we can port that over to undici. |
Beta Was this translation helpful? Give feedback.
-
If I read the error body I get the following:
|
Beta Was this translation helpful? Give feedback.
-
Issue is not node or undici related. Its a GitHub api requirement I was missing (you have to specify a user-agent): 'use strict'
import undici from 'undici'
import http from 'http'
undici.request(
'https://api.github.com/zen',
{
headers: [
'user-agent', 'undici'
]
},
(error, { statusCode, headers, body }) => {
if (error) {
console.error(error)
process.exit(1)
}
console.log(`Staus: ${statusCode}`)
console.log(`Status Message: ${http.STATUS_CODES[statusCode]}`)
console.log('Headers', headers)
const bufs = []
body.on('data', buf => { bufs.push(buf) })
body.on('end', () => {
console.log(Buffer.concat(bufs).toString('utf-8'))
})
}
) |
Beta Was this translation helpful? Give feedback.
Issue is not node or undici related. Its a GitHub api requirement I was missing (you have to specify a user-agent):