I am using node-libcurl to post a JSON to a URL and receive back a JSON.
I tested the CURL on PHP and it works fine.
The PHP code is the following
$data = array('user' => $us, 'pwd' => $cl);
$data_string = json_encode($data);
$ch = curl_init('http://52.234.214.55:9001/autenticacion/externo');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
Now i want to make it work on a node route that looks like this:
app.post('/login2',function(req,res){
//formato JSON ACHS
var cvalue = {
'user': 'user',
'pwd': 'pass'
}
var curl = new Curl();
curl.setOpt('URL', 'http://52.234.214.55:9001/autenticacion/externo');
curl.setOpt('CURLOPT_CUSTOMREQUEST', "POST");
curl.setOpt('CURLOPT_POSTFIELDS', cvalue);
curl.setOpt('CURLOPT_RETURNTRANSFER', true);
curl.setOpt('CURLOPT_HTTPHEADER', array(
'Content-Type: application/json',
'Content-Length: ' . strlen(cvalue)));
curl.on('end', function(statusCode, body, headers) {
console.info(statusCode);
console.info('---');
console.info(body.length);
console.info('---');
console.info(this.getInfo( 'TOTAL_TIME'));
this.close();
});
curl.on('error', curl.close.bind(curl));
curl.perform();
})
This does not work i get the following error:
Unknown option given. First argument must be the option internal id or the option name. You can use the Curl.option constants. at Curl.setOpt (C:\\Users\\Ingolf\\workspace\\w2_achs2\\node_modules\\node-libcurl\\lib\\Curl.js:980:11) at C:\\Users\\Ingolf\\workspace\\w2_achs2\\index.js:130:10
this points to
curl.setOpt('CURLOPT_CUSTOMREQUEST', "POST");
As suggested and found in documentation i changed it to:
curl.setOpt(Curl.option.URL, 'http://52.234.214.55:9001/autenticacion/externo');
curl.setOpt(Curl.option.CUSTOMREQUEST, "POST");
curl.setOpt(Curl.option.CURLOPT_POSTFIELDS, cvalue);
curl.setOpt(Curl.option.CURLOPT_RETURNTRANSFER, true);
curl.setOpt(Curl.option.CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen(cvalue)));
this gets the error:
Unknown option given. First argument must be the option internal id or the option name. You can use the Curl.option constants. at Curl.setOpt (C:\\Users\\Ingolf\\workspace\\w2_achs2\\node_modules\\node-libcurl\\lib\\Curl.js:980:11) at C:\\Users\\Ingolf\\workspace\\w2_achs2\\index.js:131:10
wich points to
curl.setOpt(Curl.option.CURLOPT_POSTFIELDS, cvalue);
thanks for helping.
Check out the node-libcurl
documentation: https://github.com/JCMais/node-libcurl/blob/HEAD/api.md#curloption--enum
More specifically, you need to be using the enumerated options rather the cURL compatible options string:
CURLOPT_URL becomes Curl.option.URL
Try this:
curl.setOpt(Curl.option.CUSTOMREQUEST, "POST");
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.