This is going to be just a quick entry today, and when I was thinking of what to write about, it dawned on me just how much research I had gone in to in making different types of requests (https) in Node. Took me a while to first have it so it was able to switch between a GET and a POST of JSON data, but then when I needed to make changes on how I would post Form data (including a file attachment) was another kettle of fish - steam rolled at every turn.
So - let me help take you out of your misery. Here is my base function and its required required libraries:
An example of using this would go like follows.
form = {
file: '@/tmp/path/to/filename.txt',
comment: ''
};
request("POST","www.server.com","/",{},{'Custom-Header':'Custom-Value'},form)
.then(res=>{
//console.log(res);
//...
})
.catch(err=>{console.log(`Request Error: ${err}`)});
This will send form data, including the filename you specified in to the request. If instead you wanted to send raw JSON, you can do so by placing the object in the 3rd parameter. And of course, for only a GET you only need to use the first 3 parameters.
Hopefully this takes the pain out of your day!
Comments
Post a Comment