[英]Using file content as object data
I'm trying to send a http request using formData
from a.json file but can't get the data to add into the options
object correctly.我正在尝试使用来自
formData
文件的 formData 发送 http 请求,但无法将数据正确添加到options
object 中。
{"name":"charlie","age":"20"}
Below is what I've got going at the moment.以下是我目前要做的事情。
fs.readFile(__dirname + directory, (error, data) => {
if(error) {throw error;}
data = JSON.stringify(JSON.parse(data))
var options = {
'method': 'GET',
'url': 'https://google.com',
formData: data
};
.............
});
This isn't working as I intend as it's seeing data
as a string putting apostrophe's around the content.这不像我想要的那样工作,因为它将
data
视为在内容周围放置撇号的字符串。 I've tried to use Object.assign
but again I run into the issue that my data
is seen as a string.我尝试使用
Object.assign
但我再次遇到我的data
被视为字符串的问题。
Any help would be greatly appreciated, I've been trying to research this for the past 2 hours and haven't had much luck.任何帮助将不胜感激,在过去的 2 个小时里,我一直在尝试对此进行研究,但运气不佳。
If you have any recommendations on a better system or package other than request.js I'd be happy to hear.如果您对 request.js 以外的更好的系统或 package 有任何建议,我很乐意听到。
Thanks again for taking the time.再次感谢您抽出宝贵时间。
The issue is.... formData: needs an variable with Formdata as his Datatype: this should work:)问题是.... formData:需要一个以 Formdata 作为他的数据类型的变量:这应该可以工作:)
const fs = require('fs')
fs.readFile("C:/tes/test.json", (error, data) => {
if(error) {throw error;}
let fdata = FormData()
console.log(data)
data = JSON.parse(data)
console.log(data)
console.log(data.name)
fdata.append("name", data.name)
fdata.append("age", data.age)
var options = {
'method': 'GET',
'url': 'https://google.com',
formData: fdata
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.