0

This is my postman request:

http/localhost:8080/sample-market/marketAPI/1234/product/publish?productOne=testing&productTwo=checking

Can anyone help me with how to do a post call through axios. productOne & productTwo are query param & 1234 is path parameter

Singh
  • 207
  • 1
  • 5
  • 14
  • You mean like insert variables into a string? –  May 14 '20 at 07:02
  • Does this answer your question? [How to interpolate variables in strings in JavaScript, without concatenation?](https://stackoverflow.com/questions/3304014/how-to-interpolate-variables-in-strings-in-javascript-without-concatenation) –  May 14 '20 at 07:02

1 Answers1

2

I think you should read the DOC first:

From : https://www.npmjs.com/package/axios

Your given URL looks like get method:

http/localhost:8080/sample-market/marketAPI/1234/product/publish?productOne=testing&productTwo=checking

But if you still want to use POST, then here you go :

axios.post('http/localhost:8080/sample-market/marketAPI/1234/product/publish', {
    productOne: 'testing',
    productTwo: 'checking'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

For GET method :

axios.get('http/localhost:8080/sample-market/marketAPI/1234/product/publish', {
    params: {
      productOne: 'testing',
      productTwo: 'checking'
    }
})

//OR direct

axios.get('http/localhost:8080/sample-market/marketAPI/1234/product/publish?productOne=testing&productTwo=checking')
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
  • So why post an answer then? –  May 14 '20 at 07:04
  • updating as per url, so he can get idea, nothing else – Vivek Doshi May 14 '20 at 07:04
  • yes its completely fine to have a post call without any body – Singh May 14 '20 at 09:12
  • @Singh, in that case, you should use GET method, POST word is for that means you are about to post , and GET is for to get data – Vivek Doshi May 14 '20 at 09:14
  • ok can you plz explain me , what is the need of query param? i have given an API which has query param and its a post call! that post has an empty body with the two query params – Singh May 14 '20 at 09:29
  • @Singh, in that case you need to change the API, and if not API then just pass whole URL without params – Vivek Doshi May 14 '20 at 09:32
  • hey, is this URL correct? `${this.ENDPOINT}/${id}/product/publish/tst` with respect to http/localhost:8080/sample-market/marketAPI/1234/product/publish/tst. ENDPOINT is "sample-market/marketAPI/" – Singh May 14 '20 at 09:56