I'm trying to upload a file to the storyblock API. This uses S3 behind the scenes. Local file uploads work, but when trying to fetch a file from an external URL I get a 411 error.
This works:
import { createReadStream } from 'fs'
import FormData from 'form-data'
const form = new FormData()
form.append('file', createReadStream('./local-file.jpg))
form.submit(...)
But when trying an external URL for the same file it doesn't:
import FormData from 'form-data'
import got, { type Got } from 'got'
const form = new FormData()
form.append('file', got.stream(externalUrl, {decompress: false})
form.submit(...)
Response:
statusCode: 411,
statusMessage: 'Length Required',
Which indicates that somehow got.stream() gives a different stream output than createReadStream()
Content-Length
header is missing that is why it's giving you 411
. The size of file being uploaded needs be determined and then include the Content-Length
header in the request. Add this before submit.
// Get the file size
const { headers } = await got.head(externalUrl);
const contentLength = headers['content-length'];
// Append the file to the form with the correct content length
form.append('file', stream, { knownLength: contentLength });