Skip to content

Instantly share code, notes, and snippets.

@Christopher-Hayes
Last active August 12, 2022 15:23
Show Gist options
  • Save Christopher-Hayes/66a193a51f536f5e7d5f658f377f1d50 to your computer and use it in GitHub Desktop.
Save Christopher-Hayes/66a193a51f536f5e7d5f658f377f1d50 to your computer and use it in GitHub Desktop.
Converting an axios.get arraybuffer request to a Got.get()
// To convert an axios.get that uses "ArrayBuffer" to a got request use the following:
// Axios (before)
const fileData = (await axios.get('file-url-here', {
responseType: 'arraybuffer'
})).data
// Got (after)
const fileData = await got.get('file-url-here', {
responseType: 'buffer'
}).buffer()
/* For reference - Got request options source code: https://github.com/sindresorhus/got/blob/main/source/core/options.ts#L2245
* And the got buffer() function: https://github.com/sindresorhus/got/blob/main/documentation/1-promise.md#promisebuffer
*
* Note the subtle difference in parenthesis. Axios probably also has a buffer / data function. you can use either method
* of grabbing a property from the response object, or use a fancy function to get what you need. Just be aware that if
* you want a property, use must use parethesis to get this property AFTER the await runs. With got, the buffer(), or if you're
* getting json, the json() function, automatically account for this and instead return a promise that goes to the await.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment