Skip to content

Instantly share code, notes, and snippets.

@leandroandrade
Created February 28, 2024 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandroandrade/6f35cafe3d4254aac3e59f8d61cd2a04 to your computer and use it in GitHub Desktop.
Save leandroandrade/6f35cafe3d4254aac3e59f8d61cd2a04 to your computer and use it in GitHub Desktop.
Sample request using undici
const { request } = require('undici')
async makeCallback (callbackUrl, method, headers, body, message) {
try {
const res = await request(callbackUrl, {
method,
headers,
body
})
if (res.statusCode >= 200 && res.statusCode < 300) {
return true
} else {
let body
if (res.headers['content-type'].indexOf('application/json') === 0) {
body = await res.body.json()
} else if (res.headers['content-type'] === 'text/plain') {
body = await res.body.text()
} else {
res.body.resume()
// not interested in the errors
res.body.on('error', () => {})
}
this.app.log.warn({ message, statusCode: res.statusCode, body }, 'callback unsuccessful, maybe retry')
return false
}
/* c8 ignore next 4 */
} catch (err) {
this.app.log.warn({ err }, 'error processing callback')
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment