Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  124] [ 4]  / answers: 1 / hits: 7125  / 4 Years ago, fri, july 24, 2020, 12:00:00

I am trying to send a video to a videosite, I am able to upload the video using the REST api and postman, so I know the api works as intended. Now I want to do exatcly the same request using axios. I have code that looks like the example on how to use form-data and axios:


const form = new FormData();
const stream = fs.createReadStream(PATH_TO_FILE);

form.append('image', stream);

// In Node.js environment you need to set boundary in the header field 'Content-Type' by calling method `getHeaders`
const formHeaders = form.getHeaders();

axios.post('http://example.com', form, {
headers: {
...formHeaders,
},
})
.then(response => response)
.catch(error => error)

I get the error that data: 'Content-Length is required'


Any ideas?


More From » node.js

 Answers
5

The solution to my problem was to set Content-Length accordingly:


"Content-Length": fs.statSync(filePath)['size']


[#3085] Wednesday, July 22, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hailey

Total Points: 355
Total Questions: 91
Total Answers: 91

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
hailey questions
Sun, Jul 11, 21, 00:00, 3 Years ago
Mon, Nov 2, 20, 00:00, 4 Years ago
Fri, Aug 30, 19, 00:00, 5 Years ago
Sun, Apr 28, 19, 00:00, 5 Years ago
;