1

How would I turn this curl request into an ajax call in javascript? (an answer in raw js or any library is fine)

curl:

curl -H "Content-Type: application/json" -d '{"foo":0, "bar": 0, "baz":"test"}' -X GET http://localhost:8080/public/v1/state/HelloWorld

The URL I tried in my ajax call (it gives a 404 error):

GET http://192.168.56.101:8080/public/v1/state/HelloWorld?foo=0&bar=0&baz=test

Code

return axios.get(switchDomain + '/public/v1/state/HelloWorld', {
    params: {
      foo: 0,
      bar: 0,
      baz: "BER",
    }
  })
  .then(function(response){
    console.log('perf response', response);
  });
3
  • The URL I tried in my ajax call - what ajax call? you haven't shown any code regarding the issue you are having Commented Aug 29, 2016 at 0:42
  • AJAX calls don't do anything special. If a GET request to 192.168.56.101:8080/public/v1/state/… fails via AJAX, it's not AJAX/JS's fault. Visit 192.168.56.101:8080/public/v1/state/… in your browser. Is there something there? Commented Aug 29, 2016 at 0:48
  • Yea just pasting the GET request into my browser fails as well. But it works with the curl call. So I'm assuming I'm not understanding something about the arguments / how the data is being passed Commented Aug 29, 2016 at 1:01

1 Answer 1

3

Seems like -d will turn your CURL request into POST regardless of your -X option, so:

var req = new  XMLHttpRequest();
req.open( "POST", "http://localhost:8080/public/v1/state/HelloWorld", true);
req.send("foo=0&bar=0&baz=test");

Eventually you may need to add a content-type header after req.open and before req.send.

req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

Or as posted in your question, you may want to send it as JSON

var req = new  XMLHttpRequest();
req.open( "POST", "http://localhost:8080/public/v1/state/HelloWorld", true);
req.setRequestHeader("Content-Type", "application/json");
req.send(JSON.stringify({
    foo: 0,
    bar: 0,
    baz: "test"
}));
Sign up to request clarification or add additional context in comments.

12 Comments

In fairness, OP might not have been a Facebook JavaScript engineer... but yeah.
@ceejayoz well, since when do you need to be a JavaScript engineer (is that a thing? :P) to replace an URL in 3 lines of code? No offense to anyone!
That's the "but yeah" part of my comment. :-p
I can handle making an ajax call (or pasting a URL with params into my browser for that matter) it's interpreting the cURL call arguments that I'm unsure of. That's why I made the GET request in ajax / my browser's URL), and when that didn't work looked up what the curl arguments were doing, and when I didn't see anything that would explain why the curl worked and ajax failed - came here to post
@DonnyP that's very weird, query string parameters are standard pretty much since the birth of the internet.
|

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.