gpt4 book ai didi

javascript - 使用 JS 和 NodeJS 上传文件 post 请求

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:11 28 4
gpt4 key购买 nike

我有以下请求,我想在nodejs中编写一个post请求:

POST /rest-api/hashDb/createAndUpload HTTP/1.1

Host: 192.168.101.54

Connection: keep-alive

Content-Length: 428

Origin: https://192.168.101.54

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryQGLkZW0bVDsvWPEY

Accept: /

Referer: https://192.168.101.54/ufed/index.html

Accept-Encoding: gzip, deflate, br

Accept-Language: en-US,en;q=0.9,es;q=0.8,he;q=0.7

Cookie: JSESSIONID=s3a5sjc6wp1y1k9hv5advlf3a

这是我编写的代码,但它不起作用(文件未上传):

var FormData = require('form-data');
var fs = require('fs');
var request = require("request")
// var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

let filePath = "C:\\Temp\\directory.txt"
let myFile = fs.readFileSync(filePath,'utf8');
var formData = new FormData();
formData.append("myFile", myFile, "directory.txt");
var bounding = {"id":null,"type":"HashDbDto","name":"jorge1","fileType":"TXT","version":"1.2","redaction":false,"categories":[],"format":"MD5"};
formData.append("myFile", JSON.stringify(bounding));

request({
headers: {'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryeiNygPyQcm8jomRB'},
url: "https://192.168.101.54/rest-api/hashDb/createAndUpload",
method: "POST",
form: formData
});

你的解决方案是什么?

最佳答案

使用formData键代替form

request({
headers: {'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryeiNygPyQcm8jomRB'},
url: "https://192.168.101.54/rest-api/hashDb/createAndUpload",
method: "POST",
form: formData
})

参见https://www.npmjs.com/package/request 。对于 multipart/form-data,我们使用 @felixge 的表单数据库。在大多数情况下,您可以通过 formData 选项传递上传表单数据。

关于javascript - 使用 JS 和 NodeJS 上传文件 post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51007728/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com