Ruslan K
Ruslan K

Reputation: 79

cURL command to Axios request with json data

I've got this cURL request working perfectly on remote interface just as it should

curl -XGET "https://server.host:8080/peregrine" -d '{"exchanges":["kucoin", "kraken"],"volume":10}' -k

I'm trying to build a little frontend app with Vue.js and need the above converted to an Axios get request. I've been trying the following so far:

axios({
      method: 'get',
      url: 'https://server.host/peregrine',
      data: {"exchanges":["kucoin", "kraken"],"volume":10}
    });

putting params instead of data makes it a URL and remote server says that it received no data.

What am I doing wrong? Thanks.

Upvotes: 8

Views: 15802

Answers (3)

Ruslan K
Ruslan K

Reputation: 79

Thanks for your replies!

Indeed there's no way to send data body with axios.get() We ended up tuning the server side to accept normal generic GET requests. Thanks again to everyone who answered!

Upvotes: -2

Wolfetto
Wolfetto

Reputation: 1130

Likely the problem could be that using GET you cannot pass data like you are doing. You have to pass them as query parameter. Try to change your call with:

axios.get('https://server.host/peregrine', {
    params: {"exchanges":["kucoin", "kraken"],"volume":10}
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .then(function () {
    // always executed
  });  

Upvotes: 5

Quentin
Quentin

Reputation: 943510

GET requests should not have request bodies.

CURL will allow you to make a GET request with one, but XMLHttpRequest and fetch (the HTTP APIs in browsers which axios wraps) will not.

Make a POST request instead. You might need to change the server-side code to support this.

Upvotes: 3

Related Questions