'Cant post to api. 400 fetch error (React)
I have an issue when i am trying to post to my API (Net6.0) because i get the error of 400 fetch, i have tried different things that i have found on here but i cant seem to fix it.
Here is my code:
my function:
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [link, setLink] = useState('');
const handleSubmit =(e) => {
e.preventDefault();
const post = { title, description, link };
fetch('https://localhost:7018/api/Posts', {
method: 'POST',
headers: { "Content-Type": "application/json" },
body: JSON.stringify(post)
}).then(() => {
console.log('new post added');
// GoBack()
})
}
and my return:
<div className="create">
<form onSubmit={handleSubmit}>
<label>Titel</label>
<input
type="text"
required
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<label>Description</label>
<textarea
required
value={description}
onChange={(e) => setDescription(e.target.value)}
></textarea>
<label>Link</label>
<input
type="text"
required
value={link}
onChange={(e) => setLink(e.target.value)}
/>
<button>Skapa Inlägg</button>
<p>{ title }</p>
<p>{ description }</p>
<p>{ link }</p>
</form>
</div>
here is the error code i get [1]: https://i.stack.imgur.com/WHwTn.png
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
