0

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"
lucaset256
  • 45
  • 1
  • 5

1 Answers1

-1

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);


Souhail HARRATI
  • 303
  • 2
  • 7
  • In this case, where should I put the "ON" statement? – lucaset256 Jan 06 '22 at 10:00
  • you can send data in body: xhr.send("ON"); – Souhail HARRATI Jan 06 '22 at 10:05
  • Thank you so much, I was able to perform the request and get a response from server. Unfortunately it was a 401 error response... I now have an API token I need to put somewhere in my javascript code (api token is to be declared as username, leaving the password field blank). Can you kindly help me with this last request as well? – lucaset256 Jan 07 '22 at 07:14
  • You are welcome, you can use this line : xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); – Souhail HARRATI Jan 07 '22 at 08:15