nodejs - first argument must be a string or Buffer - when using response.write with http.request

前端 未结 8 1923
难免孤独
难免孤独 2020-12-24 11:29

I\'m simply trying to create a node server that outputs the HTTP status of a given URL.

When I try to flush the response with res.write, I get the error: throw new T

相关标签:
8条回答
  • 2020-12-24 11:43

    Well, obviously you are trying to send something which is not a string or buffer. :) It works with console, because console accepts anything. Simple example:

    var obj = { test : "test" };
    console.log( obj ); // works
    res.write( obj ); // fails
    

    One way to convert anything to string is to do that:

    res.write( "" + obj );
    

    whenever you are trying to send something. The other way is to call .toString() method:

    res.write( obj.toString( ) );
    

    Note that it still might not be what you are looking for. You should always pass strings/buffers to .write without such tricks.

    As a side note: I assume that request is a asynchronous operation. If that's the case, then res.end(); will be called before any writing, i.e. any writing will fail anyway ( because the connection will be closed at that point ). Move that line into the handler:

    request({
        uri: 'http://www.google.com',
        method: 'GET',
        maxRedirects:3
    }, function(error, response, body) {
        if (!error) {
            res.write(response.statusCode);
        } else {
            //response.end(error);
            res.write(error);
        }
        res.end( );
    });
    
    0 讨论(0)
  • 2020-12-24 11:44

    And there is another possibility (not in this case) when working with ajax(XMLhttpRequest), while sending information back to the client end you should use res.send(responsetext) instead of res.end(responsetext)

    0 讨论(0)
  • 2020-12-24 11:47

    Although the question is solved, sharing knowledge for clarification of the correct meaning of the error.

    The error says that the parameter needed to the concerned breaking function is not in the required format i.e. string or Buffer

    The solution is to change the parameter to string

    breakingFunction(JSON.stringify(offendingParameter), ... other params...);
    

    or buffer

    breakingFunction(BSON.serialize(offendingParameter), ... other params...);
    
    0 讨论(0)
  • 2020-12-24 11:49

    I get this error message and it mentions options.body

    I had this originally

    request.post({
        url: apiServerBaseUrl + '/v1/verify',
        body: {
            email: req.user.email
        }
    });
    

    I changed it to this:

    request.post({
        url: apiServerBaseUrl + '/v1/verify',
        body: JSON.stringify({
            email: req.user.email
        })
    });
    

    and it seems to work now without the error message...seems like bug though.

    I think this is the more official way to do it:

     request.post({
            url: apiServerBaseUrl + '/v1/verify',
            json: true,
            body: {
                email: req.user.email
            }
        });
    
    0 讨论(0)
  • 2020-12-24 11:53

    The first argument must be one of type string or Buffer. Received type object

     at write_
    

    I was getting like the above error while I passing body data to the request module.

    I have passed another parameter that is JSON: true and its working.

    var option={
    url:"https://myfirstwebsite/v1/appdata",
    json:true,
    body:{name:'xyz',age:30},
    headers://my credential
    }
    rp(option)
    .then((res)=>{
    res.send({response:res});})
    .catch((error)=>{
    res.send({response:error});})
    
    0 讨论(0)
  • 2020-12-24 11:54

    response.statusCode is a number, e.g. response.statusCode === 200, not '200'. As the error message says, write expects a string or Buffer object, so you must convert it.

    res.write(response.statusCode.toString());
    

    You are also correct about your callback comment though. res.end(); should be inside the callback, just below your write calls.

    0 讨论(0)
提交回复
热议问题