Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

451
Visualizações
Need to convert PHP Curl Request to JavaScript (Node.js)

Currently I am having Curl request in PHP like below,

$url = "http://localhost:8500/v1/catalog/service/api?passing&tag=%2Fu%2Fdeepak%2F211%2Flocalhost"
$ch = curl_init($url);

function _exec($ch) {
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $response = curl_exec($ch);
    $info  = curl_getinfo($ch);
    if ($info['http_code'] !== 200) {
            if ($info['http_code'] === 404) {
                throw new Exception('Unable to find requested requested Consul API: ' . $info['url']);
            } else {
                $message = 'Url: ' . $info['url'];
                $error = curl_error($ch);
                if (! empty($response)) {
                    $message .= ' -- Response: ' . $response;
                }
                if (! empty($error)) {
                    $message .= ' -- Error: ' . $error;
                }
                throw new Exception('Failed to call Consul API: ' . $message);
            }
        }
        curl_close($ch);
        return json_decode($response);
}

Need to convert this same code into JavaScript and run it on Node.js. Mostly I found solutions using only using AJAX. Any help would be much appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use JavaScripts Fetch API (available in your browser) to make network requests.

If using node, you will need to install the node-fetch package.

const url = "http://localhost:8500/v1/catalog/service/api?passing&tag=%2Fu%2Fdeepak%2F211%2Flocalhost";
    
    const options = {
      headers: {
        //Authorization: "Bearer 6Q************"
      }
    };
    
    fetch(url, options)
      .then( res => res.json() )
      .then( data => console.log(data) );
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda