Created
April 26, 2022 19:55
-
-
Save coryarmbrecht/0b6c25b420af73b4d71ff3ecb44364d4 to your computer and use it in GitHub Desktop.
GET a webp image with Axios, convert stream to jpeg, and add to form for POST.
This file contains hidden or 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 form = new FormData(); | |
| const url = 'https://i.ytimg.com/vi/20u8EOBbmSk/hqdefault.jpg?sqp=-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBKHG0GyzmzLNJ-04e5KSIcXCJtBQ' | |
| // Get an image from YouTube and return arraybuffer or stream | |
| const getImageStream = await axios.get(`${url}`, { responseType: "arraybuffer" }) | |
| .then(async (data)=>{ | |
| const buffer = Buffer.from(data, 'binary') | |
| /* // Doesn't work | |
| // Get error: "Error: Input buffer has corrupt header:" | |
| const convertedData = await sharp(buffer) | |
| .jpeg({ mozjpeg: true }) | |
| .toBuffer(); | |
| */ | |
| // Add returned data to form | |
| form.append('file', data.data) | |
| await axios.post(differentURL,form) | |
| .then( (response) => console.log('Success! response.data = ', response.data) ) | |
| .catch(error => console.log('error = ', error)); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment