Reputation: 143
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.
{"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. I've tried to use Object.assign
but again I run into the issue that my data
is seen as a string.
Any help would be greatly appreciated, I've been trying to research this for the past 2 hours and haven't had much luck.
If you have any recommendations on a better system or package other than request.js I'd be happy to hear.
Thanks again for taking the time.
Upvotes: 0
Views: 63
Reputation: 95
The issue is.... formData: needs an variable with Formdata as his Datatype: this should work :)
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
};
});
Upvotes: 1