javascript - Passing params to POST call with axios - Stack Overflow

I'm trying to add the following query param into my axios call, but for some reason it's not

I'm trying to add the following query param into my axios call, but for some reason it's not accepting it.

const config = {
  headers: {
    'Content-Type': 'application/json',
  }
}

const params = {
  params: {
    isAccepted: true
  }
}

const url = ``
axios.post(url, params, config)

However it works perfectly fine when I do it in this format

const config = {
  headers: {
    'Content-Type': 'application/json',
  }
}

const url = ``
axios.post(url, params, config)

Am I using the params field wrong in the axios call?

I'm trying to add the following query param into my axios call, but for some reason it's not accepting it.

const config = {
  headers: {
    'Content-Type': 'application/json',
  }
}

const params = {
  params: {
    isAccepted: true
  }
}

const url = `https://testapi.`
axios.post(url, params, config)

However it works perfectly fine when I do it in this format

const config = {
  headers: {
    'Content-Type': 'application/json',
  }
}

const url = `https://testapi.?isAccepted=true`
axios.post(url, params, config)

Am I using the params field wrong in the axios call?

Share Improve this question asked Oct 12, 2020 at 20:53 JorahFriendzoneJorahFriendzone 4331 gold badge7 silver badges23 bronze badges 5
  • the request parameters are only added to the URL in a query string for GET requests, for POST they're included in the request body. A sensible API shouldn't really pay attention to query parameters in the URL for a POST request. – Robin Zigmond Commented Oct 12, 2020 at 20:55
  • Yes you should add the params this way *** const params = { isAccepted: true } *** – Rilla Commented Oct 12, 2020 at 21:16
  • @RobinZigmond So in my case, the POST is paying attention to the query parameters. What should I do in this case? – JorahFriendzone Commented Oct 12, 2020 at 21:24
  • What's wrong with your second snippet, which you say works? – Robin Zigmond Commented Oct 12, 2020 at 21:28
  • You don't need to set the Content-type: application/json header since that is the default – Phil Commented Oct 12, 2020 at 23:08
Add a ment  | 

1 Answer 1

Reset to default 3

'As can be seen in documentation, axios post request can accept params in config. You are trying to pass parameters on place where your post object should be (data). Parameters can be included in config though.

axios.post(url[, data[, config]])

Here is the documentation. https://github./axios/axios#axios-api

Try restructuring the code like this

const config = {
  headers: {
    'Content-Type': 'application/json',
  },
  params: {
     isAccepted: true
  }
}

const data = {
   data: 'whatever'
}

axios.post(url, data, config)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745251368a4618694.html

相关推荐

  • javascript - Passing params to POST call with axios - Stack Overflow

    I'm trying to add the following query param into my axios call, but for some reason it's not

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信