Stream API Upload Through a Proxy Service

I am trying to upload a video mp4 file through a proxy service in node.js

I want to run this on cloudflare workers at the edge of the network

export async function uploadVideo(file_buffer: any) {
const formData = new FormData()
formData.append(‘file’, file_buffer)

const headers = {
…formData.getHeaders(),
‘Content-Length’: formData.getLengthSync(),
‘X-Auth-Key’: config.CLOUDFLARE_AUTH,
‘X-Auth-Email’: config.CLOUDFLARE_EMAIL
}
// Now send the request
return await axios.post(${config.CLOUDFLARE_BASE_URL}/client/v4/accounts/{accountId}/stream, formData, { headers })
.then(response => response.data)
.catch(err => console.error(err))
}

I continue to receive back a 400, bad request, which likely means its malformed.

Any tips?

The 400 response will likely have a message in the response body. Sharing that could help us resolve your issue.

1 Like

Nothing in the body it doesn’t look like…

response: {
status: 400,
statusText: ‘Bad Request’,
headers: {
date: ‘Wed, 26 Aug 2020 19:38:49 GMT’,
‘content-type’: ‘application/json; charset=UTF-8’,
‘transfer-encoding’: ‘chunked’,
connection: ‘close’,
‘set-cookie’: [Array],
vary: ‘Accept-Encoding’,
‘cf-cache-status’: ‘DYNAMIC’,
‘cf-request-id’: ‘04cde0bfd90000c9b110075200000001’,

},
config: {
url: {secret}
method: ‘post’,
data: [FormData],
headers: [Object],
transformRequest: [Array],
transformResponse: [Array],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: ‘XSRF-TOKEN’,
xsrfHeaderName: ‘X-XSRF-TOKEN’,
maxContentLength: -1,
validateStatus: [Function: validateStatus]
},
request: <ref *1> ClientRequest {
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
outputData: ,
outputSize: 0,
writable: true,
destroyed: false,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: true,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: null,
_hasBody: true,
_trailer: ‘’,
finished: true,
_headerSent: true,
socket: [TLSSocket],
_header: ‘POST {secret} HTTP/1.1\r\n’ +
‘Accept: application/json, text/plain, /\r\n’ +
‘Content-Type: multipart/form-data; boundary=--------------------------543277250383579290208759\r\n’ +
‘Content-Length: 3114573\r\n’ +
‘X-Auth-Key {secret}’ +
‘X-Auth-Email: {secret}’ +
‘User-Agent: axios/0.19.2\r\n’ +
‘Host: api.cloudflare.com\r\n’ +
‘Connection: close\r\n’ +
‘\r\n’,
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: ‘POST’,
maxHeaderSize: undefined,
insecureHTTPParser: undefined,
path: ‘{secret}’,
_ended: true,
res: [IncomingMessage],
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
reusedSocket: false,
host: ‘api.cloudflare.com’,
protocol: ‘https:’,
_redirectable: [Writable],
[Symbol(kCapture)]: false,
[Symbol(kNeedDrain)]: true,
[Symbol(corked)]: 0,
[Symbol(kOutHeaders)]: [Object: null prototype]
},
data: { result: null, success: false, errors: [Array], messages: null }
},
isAxiosError: true,
toJSON: [Function (anonymous)]
}

"
“message”:“Request failed with status code 400”,“name”:“Error”,“stack”:"Error: Request failed with status code 400\n

"

Got it! It’s very hard for me help without replicating the issue with my code. Could you give me time over the weekend so I can try to hit the same issue as you and post my code here?

Yes, that would be incredible.

Specifically, I am using:

Axios for sending the request,
and placing the video into an array buffer per the code above. If you find another way that works using node, then that is okay with me. The hope is that I will use this to horizontally scale thousands of videos uploads into Cloudflare stream at the edge daily, and to reusue the code to send to AWS S3 for images since cloudflare doesn’t have an image storage service (yet).

I have “saved” the video to a mock local server using node’s file system driver and the video itself does not seem to be malformed in any way as I can replay it exactly at the same video quality.