2

How can I translate this curl script to an AJAX request in JavaScript?

curl -X POST
     -d "grant_type=password&username=admin&password=Demo1234"
     -u "<ClientID>:<ClientSecret> " http://<host>/url/to/auth
1
  • It should be noted that doing this will expose these values to users. In most cases that include a password parameter, this is not a good thing. Commented Dec 21, 2017 at 21:42

3 Answers 3

3

I will show an example in pure JavaScript

function sendData()
{
    var formData = new FormData(); //create formData object to send data
        formData.append("grant_type, "password"); //via append you add data 
        formData.append("username", "admin"); 
        formData.append("password", "Demo1234");
    var xmlHttp = new XMLHttpRequest(); //create "ajax/xhr" object
        xmlHttp.onreadystatechange = function() //monitor status of response
        {
            if(xmlHttp.readyState === 4 && xmlHttp.status === 200) //if it's ok
            {
                console.log(xmlHttp.responseText); //then output data
            }
        }
        xmlHttp.open("POST", "http://<host>/url/to/auth"); 
        xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("<ClientID>" + ":" + "<ClientSecret>");
        xmlHttp.send(formData);

}
Sign up to request clarification or add additional context in comments.

Comments

2

Your curl call uses three things:

  1. Unprocessed data.
  2. HTTP Authentication.
  3. Templating? - Not sure.

This is what I best came up with:

$.ajax({
  "url": "http://<host>/url/to/auth",
  "data": "grant_type=password&username=admin&password=Demo1234",
  "processData": false,
  "beforeSend": function (xhr) {
    xhr.setRequestHeader ("Authorization", "Basic " + btoa("<ClientID>" + ":" + "<ClientSecret>"));
  }
});

Replace your <ClientID> and <ClientSecret> with the right values.

9 Comments

Important to note that you can do this with plain javascript. You don't need to use Jquery although that does make it much easier.
@richbai90 Yep... Also this method is cross browser... Please note that the author asks for jQuery method in tags. :)
@richbai90 not really, native XHR is arguably better now a days, especially if you can use fetch
@SterlingArcher No support in IE 7! :) Remember banks still use IE 6!!!
There's far more development jobs out there than there are developers
|
1

Here is how you can do it for any curl not just this one:

  1. Get postman (https://www.getpostman.com/)
  2. follow this to import your curl to postman: https://www.getpostman.com/docs/postman/collections/data_formats#importing-postman-data
  3. follow this to generate a code snippet for the curl you just imported: https://www.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets

one of the code snippet exporters is jqueryAjax.

1 Comment

3 step is thing useful has made my day xD Thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.