简体   繁体   中英

Using npm's request package, how should you pass data originally held in a file?

I'm using npm's request and I'm working with an API. The API request over CURL required that I send information through a file using @site.json.

Example:

curl -X POST -d @site.json 'https://api.xxxxxxx.com/site?id=123456’

Here's the main part of my code:

let data = JSON.stringify(`{"placement":{"name":"${placementName}"}}`);

fs.writeFile('placement.json', JSON.parse(data), (err, data) => {
    if (err) console.log(err);
})

fs.createReadStream('placement.json').pipe(request(options));
// clear the file for next placement
fs.truncate('placement.json', 0, () => {console.log('done')}) 

So I am able to build what I need to using this code and a function I created to make sure no duplicates are created. The problem is that I need to run the script multiple times in order to get all the "placements" built.

I am guessing it's the fs package and it's not working fast enough or failing on writing the file at times. I can see the .json file and sometimes the data is not cleared (but is overwritten) and sometimes the code does not update when it should.

Is there a way to pass this json data through using "request" if it originally was contained in a file? I searched the documentation but haven't found the answer yet.

I've tried a few ways in options like using json, data, forms, and the API doesn't seem to recognize the information sent in that way. Is there another way I should try?

My options:

const options = {
    url: `https://api.xxxxxxxxxx.com/placement?id=${publisherId}`, 
    method: 'POST',
    headers: {
        'Authorization': token
    }
};

Thanks

The fs.writeFile is asynchronous function which means if you want to work with the file you need to do it inside its callback which is called after the file was writen to disk, that's where you have if (err) console.log(err); .

But i think that you don't need to write the file to disk at all.

You can simply pass the data directly to request like this:

var request = require('request');

const options = {
    url: `https://api.xxxxxxxxxx.com/placement?id=${publisherId}`, 
    method: 'POST',
    headers: {
        'Authorization': token
    },
    json: true,
    body: { placement: { name: placementName } }
};

request(options, function callback(error, response, body) {
     //...
});

I haven't tried the code, though.

I have the answer, so the json data or any data that was part of a file, can be passed through options.

I used the body option. I tried that last night but for some reason, it didn't work.

I probably missed a step but needed to stringify and then parse the JSON data in order to have the api recognize the code correctly. Now all my actions are done when they are needed on the first call.

body: JSON.parse(JSON.stringify(`{"placement":{"name":"${placementName}"}}`))

Thanks

Edit: So this ended up working.

body: `{"placement":{"name":"${placementName}"}}`

I am thinking that Request handles all the parse / stringify should it be needed in the data you pass through options. Didn't have to declare JSON either. This is a game changer for me as I don't have to use fs in many parts of my code now.

Thanks for the comments guys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM