Created
February 28, 2024 17:14
-
-
Save leandroandrade/6f35cafe3d4254aac3e59f8d61cd2a04 to your computer and use it in GitHub Desktop.
Sample request using undici
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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