mercredi 20 novembre 2019

Trying to post json to node JS server with curl but times out

Hey,

I'm working on a Facebook messenger bot that is hosted on a nodejs server and accesses data from a custom API.

I'm trying to send send data to my nodejs server with curl. What happens is that I receive the data, and I'm able send a messenger message with that data but in the background my curl request times out.


Here's my curl request

    $ch = curl_init('some url');
    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))
    );
    curl_setopt($ch, CURLOPT_TIMEOUT, 2);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);

    $res  = curl_exec($ch);
    curl_close($ch);


Here's how I receive it with nodeJS

app.post('/notify', (req, res) => {

    if (req.body.notify > 0) {
        var user = {
          id: req.body.sender_id
        }
        console.log("value of id extern : " + user.id);
        var text = [];

        text[0] = {"text": req.body.textValue.replace(/<[^>]*>?/gm, '')};

        call.SendAction(user);
        call.SendApi(user, text);
    }
});


This is the error I get

at=error code=H12 desc="Request timeout" method=POST path="/notify" host=team-bot-4858.herokuapp.com request_id=818b120b-4305-4aa6-ba4d-80dff02f61b9 fwd="176.162.183.225" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0 protocol=http

EDIT

  • I tried to increase the timeout for my curl request by setting CURLOPT_CONNECTTIMEOUT to 0 and CURLOPT_TIMEOUT to 400 but it didn't change the result still having timeouts.
  • After some digging around in my error_log file I found the cause of timeout (I think...) the error is below.
curl_exec(): supplied resource is not a valid cURL handle resource

PS :

  • I'm fairly new to web development so my choice of words or description might not be very clear

  • I apologize in advance for my English also (not my mother tongue).




Aucun commentaire:

Enregistrer un commentaire