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, forPOST
they're included in the request body. A sensible API shouldn't really pay attention to query parameters in the URL for aPOST
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
1 Answer
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
评论列表(0条)