Getting "first argument must be a string or Buffer" error

19,422

Solution 1

The error is in the request call. To submit the form correctly use

    request.post({
        url: 'http://localhost:5000/generate',
        form: formatted
    }, function(error, response, body) {
        console.log(body);
    });

I encourage you to use the npm module request-promise-native: https://github.com/request/request-promise-native https://www.npmjs.com/package/request-promise

var rp = require('request-promise');

var options = {
    method: 'POST',
    uri: 'http://api.posttestserver.com/post',
    body: {
        some: 'payload'
    },
    json: true // Automatically stringifies the body to JSON 
};

rp(options)
    .then(function (parsedBody) {
        // POST succeeded... 
    })
    .catch(function (err) {
        // POST failed... 
    });

Solution 2

your body is expected to be Buffer try this, json:true in request option

 request.post({
        headers: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        url: 'http://localhost:5000/generate',
        body: formatted,
        json:true
    }, function(error, response, body) {
        console.log(body);
    });

Solution 3

why are you writing:

router.route('/function')
  .post(function(req, res) {

try:

router.post('/function', function(req, res) {
Share:
19,422
Cyclops
Author by

Cyclops

Code Lover

Updated on June 09, 2022

Comments

  • Cyclops
    Cyclops almost 2 years
    // call the packages
    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    var figlet = require('figlet');
    var querystring = require('querystring');
    var http = require('http');
    var fs = require('fs');
    var request = require('request');
    
    // configure app to use bodyParser()
    // this will let app get the data from a POST
    app.use(bodyParser.urlencoded({
        extended: true
    }));
    app.use(bodyParser.text());
    
    // set port
    var port = process.env.PORT || 8082;
    
    // get an instance of the express Router
    var router = express.Router();
    
    
    // middleware to use for all requests
    router.use(function(req, res, next) {
    
        // do logging
        console.log('UnitGener router invoking');
        // go to the next routes and don't stop here
        next();
    });
    
    // test route to make sure everything is working (accessed at GET http://localhost:8082/api)
    router.get('/status', function(req, res) {
        res.json({
            message: 'UnitGener is ready to work!'
        });
    });
    
    
    //noinspection JSUnresolvedFunction
    router.route('/function')
    
        .post(function(req, res) {
            console.log(req.body);
            var formatted = req.body;
    
    
            request.post({
                headers: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                url: 'http://localhost:5000/generate',
                body: formatted
            }, function(error, response, body) {
                console.log(body);
            });
    
        });
    
    app.use('/api', router);
    app.listen(port);
    });
    

    Here is my complete code for creating a POST route with the given configs and then I am calling another post route within the post method. But I am getting the "throw new TypeError('first argument must be a string or Buffer');" this error. I did some google finding and did some changes also , non of them worked and still find it hard to point the error. I changed the body: formatted to body: formatted.toString() also but didn't work. Please give me some advice to find this out. Its a huge help for me.

    Thanks in Advance

  • Josh F
    Josh F over 6 years
    Thank you! I was having this issue, and adding json: true in the request options is all that was needed to fix. Much appreciated.
  • Lee O.
    Lee O. over 5 years
    I was using request-promise-native and was getting this same error. Adding the json: true to the options was what solved the issue for me.