1

I have a API that updates data in the database but then returns back a query. The API takes a single parameter and it works when testing it manually through browser and when also testing using a axios get request. However for security purposes I wanted to try and stick with POST request and to just test it out, however if I try to run this via a POST (change axios.get to axios.post) request the parameter value is not passed to the API. Here is my axios request below. What could be the issue?

                axios.post(window.location.origin + '/API/StateContacts', {
                    params: {
                        STATE_DD_INPUT: stateDD
                    }
                })
                    .then(response => {
                        this.stateContactsArray = response.data;
                    }).catch(error => {
                        console.log(error.response.data.error);
                    });

2 Answers 2

2

Axios post second parameter is already the data you are trying to pass. From

axios.post(window.location.origin + '/API/StateContacts', {
                    params: {
                        STATE_DD_INPUT: stateDD
                    }
                })

to

axios.post(window.location.origin + '/API/StateContacts', {
                        STATE_DD_INPUT: stateDD
                    })

should solve your issue

Edit

You commented:

However if I do this and add a {} it works but not sure why.

axios.post(window.location.origin + '/API/StateContacts', {}, { params: { STATE_DD_INPUT: stateDD } }) .then(response => { this.stateContactsArray = response.data; }).catch(error => { console.log(error.response.data.error); });

This is because the signature for axios.post function is:

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

Instead of passing the data as the second argument you are passing the third argument which is the config. The config object has the parameters key that will add to your request the following query parameters:

http://localhost/API/StateContacts?STATE_DD_INPUT=xxx

where the query string will be STATE_DD_INPUT.

Since we don't know your backend we are assuming that you are using a method to get the query string from the request and not the body of the request. Frameworks like Laravel looks at the query string and the body for what you searching. E.g.

$request->get('STATE_DD_INPUT')

You are probably using a framework that has two different methods. One to get the query parameters (e.g. from the URL - https://localhost/API/StateContacts?STATE_DD_INPUT=xxxx) and other to get from the body (e.g. the one sent in the body request - please, consult this docs to learn more about POST body). So, maybe somewhere in your backend code you will have

$request->body('STATE_DD_INPUT')

To be able to get the request from the query string you should have some method to do that. e.g.

$request->query('STATE_DD_INPUT')
Sign up to request clarification or add additional context in comments.

6 Comments

It does not solve it for some reason. Still not passing parameter. However if I do this and add a {} it works but not sure why. axios.post(window.location.origin + '/API/StateContacts', {}, { params: { STATE_DD_INPUT: stateDD } }) .then(response => { this.stateContactsArray = response.data; }).catch(error => { console.log(error.response.data.error); });
@Bradyboyy88 I have updated my question to answer this comment. If this answer answers your question, please mark it as the correct answer
I am going through your answer to fully understand but just for reference I am using asp.net core web api controllers.
Can you explain what this format is axios.post(url[, data[, config]]) in your post. Adding {} works but I looking at what you posted It is unclear to me this syntax. Is this saying URL contains a data array which contains a config array and my parameters are in the config array which my API must account for and the reason it is working?
And also in my web API I use FromQuery so why does the parameters in the config work?
|
0

Do you want to pass parameter in header?

axios.post(window.location.origin + '/API/StateContacts', {
    headers: {
        'Content-Type': 'application/json',
        'STATE_DD_INPUT': stateDD
    }
})      
.then((response) => {
    console.log(response.data)
})
.catch((error) => {
    console.log(error);
})

1 Comment

any reason to pass it in the header vs something else (assuming body)? Relatively new to http requests so not sure if one is favored over the other.

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.