mercredi 31 octobre 2018

Guzzle http request to nodejs server connection refuse

My Laravel PHP code is running inside vagrant homestead. Here is my PHP function that is going to send the http request to nodejs server.

private function sendcampaign($data) 
    {

        $client = new Client();

        $res = $client->request('POST', 'http://127.0.0.1:8080/sendcampaign', ['json' => $data]);

        $response = $res->getBody();

        if($res->getStatusCode() == 200) 
        {

            Log::info($response);

        } else {

            Log::error($response);

        }
    }

My NodeJS server is running independently. I run it using this command nmp start. Here is the code to my nodejs server that is accepting the request

app.post("/sendcampaign", function(request, response) {
  if (request.body) {

    var campaign = JSON.stringify(request.body);

    response.status(200);
    response.send({
      status: "success",
      message: "Campaign sent successfully."
    });

  } else {
    response.status(404);
    response.send({ status: "error", message: "Campaign failed." });
  }

  return;
});

However when I run it, I always getting an error

cURL error 7: Failed to connect to 127.0.0.1 port 8080: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

But I already tried it in Postman the response is

{"status":"success","message":"Campaign sent successfully."}

I don't know what's the error on my PHP code using GuzzleHttp. Maybe I need to add some header options? What header options should I add? Can you help me?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire