簡體   English   中英

CURL 命令到 NodeJS - PUT 請求 OctetStream

[英]CURL Command to NodeJS - PUT Request OctetStream

我需要使用fetchrequestaxios網絡庫將這個 curl 命令變成 nodejs 請求

請求需要PUT文件數據到URL

curl -v -H "Content-Type:application/octet-stream" --upload-file C:/Users/deanv/Pictures/test3.mp4 "https://www.example.com/file/upload"

我嘗試使用CurlConverter將我的 curl 命令轉換為節點代碼,但這不起作用,因為我無法添加文件數據。

var fetch = require('node-fetch');

fetch('https://www.example.com/file/upload', {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/octet-stream'
    }
});

所有幫助表示贊賞。 提前致謝。

嘗試這個:

// setup modules
const fetch = require('node-fetch');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');

// setup paths
const pathToFile = 'C:/Users/deanv/Pictures/test3.mp4';
const uploadUrl = 'https://www.example.com/file/upload';

// create form, 'content-type' header will be multipart/form-data
const form = new FormData();

// read the file as stream
const fileStream = fs.createReadStream(path.resolve(pathToFile));

// add the file to the form
form.append('my_file', fileStream);

fetch(uploadUrl, {
        method: 'PUT',
        body: form
    }).then(res => {
        console.log('done', res.status);
    })
    .catch(err => {
        console.log('err', err);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM