I am using Formik and React to POST to an .NetCore Entity Framework API.
In Postman, it POSTs fine with no errors. However, when doing a POST from a web browser, I am getting an error.
After examining both the Postman request and the request sent by the browser, the only difference I see is this:
Postman works - no quote around the value:
"galaxyTypeId": 1,
Axios Post - it puts quotes around the value:
"galaxyTypeId": "4",
I get this error in the browser:
POST https://localhost:44376/api/getformdata 400
createError.js:16 Uncaught (in promise) Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)
Here is the relevant section of code:
<Formik
initialValues={{
gameAdminEmail: "",
adminName: "",
phone: "",
galaxyTypeId: 1,
starId: 1,
description: "",
gameUrl: "",
}}
onSubmit={async values => {
await new Promise(resolve => setTimeout(resolve, 500));
axios({
method: "POST",
url: "https://localhost:44376/api/getformdata",
data: values
});
console.log(JSON.stringify(values, null, 2));
}}
>
Is there a way to prevent Axios from putting quotes around the number values?
Thanks!
I'm looking at this codepen here and the issue isn't being reproduced. https://codepen.io/OpenGG/pen/xaGKEP
var payload = { "galaxyTypeId": 45 }
axios.post('//httpbin.org/post', payload)
.then(res => {
const pre = document.createElement('pre');
pre.textContent = JSON.stringify(res.data, null, ' ');
document.body.appendChild(pre);
})
.catch(err => {
document.body.textContent = err.stack;
});
Do you have a link to the repo?