0
Giving the following error code:
TypeError: First argument must be a string or Buffer
My code is like this:
request: function (dataJson, callback) {
var options = {
hostname: 'apisandbox.cieloecommerce.cielo.com.br',
port: 443,
secureProtocol: 'TLSv1_method',
encoding: 'utf-8',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'Content-Length': 0
},
method: 'POST'
};
if (ambienteDeProducao || false) {
options.hostname = 'api.cieloecommerce.cielo.com.br';
}
var postData = dataJson;
options.headers['Content-Length'] = Buffer.byteLength(postData);
var req = https.request(options, function (res) {
var data = [];
res.on('data', function (chunk) {
data.push(chunk);
});
res.on('end', function () {
callback(null, data.join(''))
});
});
req.write(postData);
req.end();
}
I don’t know what it could be
You know the wrong line?
– Sergio
req.write(Postdata); When executing this command.
– Erick Zanetti
That one
req
is "whose"? You’re using what as a server?– Sergio
the
req
is what I use to perform the function, I’m using Node.js– Erick Zanetti
You are using the native Node server or a library like Express?
– Sergio
I use the native Node method.
– Erick Zanetti
Okay, and this one
postData
is what? Thereq.write
only accepts strings or buffer.– Sergio
When I call this function I enter a json body, Postdata receives this Json and includes it in the body of my request as buffer.
options.headers['Content-Length'] = Buffer.byteLength(postData);
– Erick Zanetti
Documentation states that either string or buffer: https://nodejs.org/api/http.html#http_response_write_chunk_encoding_callback Can’t send this object as a string? or you’ll send a stream?
– Sergio
Yeah, I tried to send it like String and it worked, only problem is Cielo didn’t take it like that. At least now I’ve found the reason for the mistake, thank you very much.
– Erick Zanetti
Ciel is the right server-side software? maybe you need headers for authentication?
– Sergio
Yes, I will read the documentation of Cielo see if I can get some answer.
– Erick Zanetti