I am trying to post comments using axios. When I submit my datas entered in the form, I see this error in the console :
AxiosError {message: 'Request failed with status code 400', name: 'AxiosError', code: 'ERR_BAD_REQUEST', config: {…}, request: XMLHttpRequest, …}
Here is my code :
import React, { useState } from 'react'
import TextField from '@material-ui/core/TextField';
import { Button } from '@material-ui/core'
import CommentsAPI from '../../Services/CommentsAPI'
export default function CommentForm() {
const [comment, setComment] = useState({})
const handleSubmit = async (event) => {
event.preventDefault();
try {
const data = CommentsAPI.create(JSON.stringify(comment))
console.log(data)
} catch (error) {
console.log(error)
}
}
const handleChange = (event) => {
const {name, value} = event.currentTarget
setComment({
...comment,
[name]: value
})
}
return (
<form onSubmit={handleSubmit}>
<div>
<TextField
id="pseudo"
label="Pseudo"
type="text"
onChange={handleChange}
name="pseudo"
/>
</div>
<div>
<TextField
id="outlined-multiline-static"
label="Comment"
multiline
minRows={2}
onChange={handleChange}
name="content"
/>
</div>
<div>
<Button variant="contained" color="primary" type="submit">
Send
</Button>
</div>
</form>
)
}
CommentsAPI.js file :
import { URL_COMMENTS } from '../config'
import axios from 'axios'
function create(comment) {
return axios.post(URL_COMMENTS, comment)
}
const CommentsAPI = {
create
}
export default CommentsAPI
I am trying to understand what is wrong. Thank you very much for your help !
Have a look on my server :