Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to run curl in javascript

#Since javascript already has functions for this purpose
#so it is not advisable to load an extra library for that
#you can achieve the same in native javascript like this
var url = "http://www.google.com";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

xhr.send();

#But still if you want to do it using curl in Node.js, it is possible
#Take a look at: https://github.com/JCMais/node-libcurl
var Curl = require( 'node-libcurl' ).Curl;

var curl = new Curl();

curl.setOpt( 'URL', 'http://www.google.com' );
curl.setOpt( 'FOLLOWLOCATION', true );

curl.on( 'end', function( statusCode, body, headers ) {

    console.info( statusCode );
    console.info( '---' );
    console.info( body.length );
    console.info( '---' );
    console.info( this.getInfo( 'TOTAL_TIME' ) );

    this.close();
});

curl.on( 'error', function ( err, errCode ) {

    //do something

    this.close();
});

curl.perform();

Comment

how to access curl data in javascript

var url = "www.yahoo.com";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

xhr.send();
Comment

PREVIOUS NEXT
Code Example
Javascript :: chamar arquivo javascript no html 
Javascript :: react disable eslint errors 
Javascript :: website edit js 
Javascript :: javascript append item to array 
Javascript :: discord.js how to edit a message 
Javascript :: javascript get random number 
Javascript :: js regex with variable 
Javascript :: update file json trong javascript 
Javascript :: how to see if the window has focus in js 
Javascript :: loop through files in directory javascript 
Javascript :: epoch time js 
Javascript :: print value in jquery 
Javascript :: javascript date method 
Javascript :: xmlhttprequest pass parameters post 
Javascript :: axios send post data 
Javascript :: send data from one page to another html page in Javascript 
Javascript :: javascript random integer 
Javascript :: javascript get nth element of array 
Javascript :: btn.addeventlistener 
Javascript :: javascript custom repeat function 
Javascript :: jquery selector this and class 
Javascript :: crop image canvas 
Javascript :: check data type in javascript 
Javascript :: sample json 
Javascript :: input in javascript 
Javascript :: angular 404 on refresh 
Javascript :: javascript remove object property 
Javascript :: jquery on click function 
Javascript :: jquery offsetheight 
Javascript :: find last element in array javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =