I'm getting this 2 errors when using post request to an API (Using chrome)
xhr.js:178 POST MY_API_URL net::ERR_HTTP2_PROTOCOL_ERROR createError.js:16 Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:83)
I'm doing a simple POST request (React code):
const postData = async()=>{
let res = await Axios.post('/terrenos', {'idTerreno':'0'} );
console.log( res.data );
}
And in the API side I just have this for debugging(PHP Code):
if( $_SERVER["REQUEST_METHOD"] === 'GET'){
main($_GET);
}else if( $_SERVER["REQUEST_METHOD"] === 'POST'){
echo(json_encode($_POST));
}
When I dont send anything in body it works just fine (It returns an empty array)
const postData = async()=>{
let res = await
Axios.post('https://gpgpy.000webhostapp.com/papaProject/API/v2/query.php');
console.log( res.data );
}
And when I use postman for the same request it with and without body it works too.
I ran into the same issue using Axios with React and a PHP web service hosted on 000webhost. It works perfectly fine with Postman as well as the vanilla fetch
function. Odd, seems to be a compatibility issue between Axios and 000webhost...
My solution was to use fetch
instead of Axios for my API calls, as I didn't see any particular reason to use Axios in my scenario.
Here's a good example on how to use fetch
with POST
.
Seems like you asked about it 3 months ago, so I hope you found a solution earlier than I could reply. If you managed to utilize Axios with 000webhost, please share your wisdom 😎