Home > front end >  How to send a curl POST request in Javascript
How to send a curl POST request in Javascript

Time:01-06

how can I send the following curl POST request in javascript? can somebody help me?

curl -X POST "https://IP" -H "accept: */*" -H "Content-Type: text/plain" -H "Authorization: Bearer XXXXX" -d "ON"

CodePudding user response:

You can use this solution:

var xhr = new XMLHttpRequest();

xhr.open("POST", "https://IP", true);


xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("accept", "*/*");
xhr.setRequestHeader("Authorization", "Bearer XXXXX");

xhr.onreadystatechange = function() { // Call a function when the state changes.
    if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
        // Request finished. Do processing here.
    }
}

xhr.send(); or xhr.send(body);


CodePudding user response:

CURL is a command in UNIX systems and a lib in PHP and just performs a HTTP request. You can use something similar in JS called Ajax-Request. An example from W3 schools

  •  Tags:  
  • Related