Node.js: Make a HTTP POST with params - node.js

call = "https://aaa#api.twilio.com/2010-04-01/Accounts/sasa/Calls.xml"
fields = { To : "+12321434", From : req.body.from }
request.post
url: call, body: fields (err,response,body) ->
console.log response.body
How can I pass fields to the HTTP POST request?
It works if I pass a string like "To=+12321434" but not "To=+12321434,From = req.body.from"

You need to stringify your data, look at the example:
http://nodejs.org/docs/latest/api/querystring.html#querystring.stringify

Related

How to send x-www-form-urlencoded body token and value in post request axios

var at = 'test' ; //This value will be received dynamically from request. Here I just used constant
var Data = 'token='+ at;
const config = {
method: 'post',
url : "https://example.org",
headers: {
"Content-Type":"application/x-www-form-urlencoded",
"Authorization":"Basic XyzI",
"Accept":"application/json"
},
data:(Data)
};
axios(config)
.then(function(response){.....}
Above is snippet from the code that I have been trying. Please note that I want to extract token from body only (see 'at' variable). From postman, I send message body as application/x-www-form-urlencoded key and value format. Please advise, how can I pass it as data in config?

Node-Red POST multipart/form-data (http request)

I would like to POST two data in multipart / form-data format using Node-RED.
(One for text data, one for voice data)
I set the function node and http request node as follows, but it does not seem to be POST.
I think that it is necessary to create a multi-part body and assign it to msg.body, but I do not know how to create a multi-part body of the voice data.
I do not know how to solve it, so someone would like to tell me.
function node
var request = global.get('requestModule');
var fs = global.get('fsModule');
msg.body = {};
msg.body = {
'apikey' : "**********",
'wav' : fs.createReadStream('/tmp/testtest.wav')
};
msg.headers = {};
msg.headers['Content-type'] = 'multipart/form-data';
return msg
http request(Property)
method ⇒ POST
URL ⇒ https://xxxxyyyzzz/
SSL/TLS ⇒ No
Basic ⇒ No
Output ⇒ JSON
The http request Node-Red core node support multipart/form-data POST out of the box.
Add a function node before your http request with this Function :
msg.headers = {};
msg.headers['Content-Type'] = 'multipart/form-data';
msg.headers['Accept'] = 'application/json';
msg.payload = {
'apikey': msg.apiKey,
'wav': {
value: msg.payload.invoice.file,
options: {
filename: 'testtest.wav',
contentType: 'audio/wav', // This is optionnal
}
}
}
return msg;
The http request node use Request nodejs library under the hood, and this one use form-data library for handling multipart, so all the options supported by these works.
The source code of the relevant part of http request handling multipart.

How to parse body params on AWS lambda?

On my AWS lambda function i have access to an event json which holds a parameter called: body. the issue is this is a raw body string (not parsed into individual parameters).
{
input: {
body: "------WebKitFormBoundarys3wLu6HlaCBrIExe\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n------WebKitFormBoundarys3wLu6HlaCBrIExe\r\nContent-Disposition: form-data; name=\"media[]\"\r\n\r\nhthtth\r\n------WebKitFormBoundarys3wLu6HlaCBrIExe\r\nContent-Disposition: form-data; name=\"media[]\"\r\n\r\nlololol\r\n------WebKitFormBoundarys3wLu6HlaCBrIExe--\r\n"
}
}
I'd like to take that and turn into:
{
foo: 'bar',
media: [
"grgkoerpkge",
"twepgbopcib"
]
}
I'd prefer not to use some bloated express server just to parse a body string.
P.S. I've tried to use body-parser but it seems like it only works with express as a middleware
const {URLSearchParams} = require('url')
const sp = new URLSearchParams(event.body)
Encountered similar issue recently, as content-type in request was plain text format, I used querystring, built in module in node js to parse body string,
more about querystring
const querystring = require('querystring');
& in lambda handler,
var jsonData = querystring.parse(event.body);
Send it to your lambda as JSON.
Then, in your lambda (if you use lambda-proxy integration), parse it by using JSON.parse(event.body).
You'r passing the params by form or with the "Content-Type" in the header "application/x-www-form-urlencoded".
You shoukd pass it with "application/json"

use node.js send request for baidu map API get wrong response?

Im trying to use node.js to send http requst and call baidu map API.
my code in blow:
If you past the url and use browser directly, it will give right response in right format.
But when I use node to send request, I get problem.
var request = require('request');
request(
{ method: 'GET',
uri: 'http://api.map.baidu.com/place/v2/suggestion?query=beijing&region=131&output=json&ak=****hLQKu9ap9fPq5N1ExF1Kk7xe5Eah'
}
, function (error, response, body) {
res.json({
res:response
})
}
)
Meanwhile, if I change the url contains some Chinese like:
http://api.map.baidu.com/place/v2/suggestion?query=北京理工大学&region=北京&output=json&ak=****hLQKu9ap9fPq5N1ExF1Kk7xe5Eah
In node.js it will give status code 400 and totally wrong response.
you must encode your uri with encodeURI
uri: encodeURI('http://api.map.baidu.com/place/v2/suggestion?query=北京理工大学&region=北京&output=json&ak=3104hLQKu9ap9fPq5N1ExF1Kk7xe5Eah')

How to get the request parameters using get in Spark Java framework?

I'm new to sparkjava. I want to read my request params using spark java but I'm not able to find the correct syntax. please help me out. Below is my route method and the client call to it:
my client request url:
/smartapp/getDataViewModelConfig?collId=123'
Route Method:
get("smartapp/getDataViewModelConfig/:id", "application/json", (request, response)
-> {
String id = request.params(":id");
}
The 'id' field is returning null here. Any suggestions as to what went wrong here?
If you have to work with an URL like /smartapp/getDataViewModelConfig?collId=123 you have to deal with query parameters in your implementation, like the following:
get("smartapp/getDataViewModelConfig", "application/json", (request, response)->{
String id = request.queryParams("collId");
return "HI " + id;
}
If you have an URL like : http://localhost:4567/smartapp/getDataViewModelConfig/456
use the following code :
get("/smartapp/getDataViewModelConfig/:id","application/json", ((request, response) -> {
response.type("application/json")
return request.params(":id");
}), gson::toJson);

Resources