Request to Cielo with Node.js

Asked

Viewed 233 times

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?

  • req.write(Postdata); When executing this command.

  • That one req is "whose"? You’re using what as a server?

  • the req is what I use to perform the function, I’m using Node.js

  • You are using the native Node server or a library like Express?

  • I use the native Node method.

  • 1

    Okay, and this one postData is what? The req.write only accepts strings or buffer.

  • 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);

  • 1

    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?

  • 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.

  • Ciel is the right server-side software? maybe you need headers for authentication?

  • Yes, I will read the documentation of Cielo see if I can get some answer.

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.