Reputation: 53
Im trying to fetch an give data to a POST-method getNumbers in the server, that has a parameter id, which simply returns the id for debugging purposes. So in the Client i do as following:
documentClickHandler=(number)=> {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: number })
};
fetch("http://localhost:5000/api/Documents/GetNumber", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => { console.log('request failed', error); });
}
Which only has the purpuse to give the id to the getNumber method in the server:
[HttpPost("GetNumber")]
public string GetNumber([FromBody] string id)
return id;
}
This gives me an error (400 bad request) and I dont know how to solve it. Any ideas? Im in desperate need of help.
Upvotes: 3
Views: 1472
Reputation: 297
Pl find here a sample to call your HttpPost method through Postman:
Just pass your body:
body: JSON.stringify(id)
Upvotes: 2
Reputation: 43860
Remove word "number" from your code. It is a reserved word
try this code:
documentClickHandler=(documentId)=> {
var id="123"; // try this value at first, after this use your value
const requestOptions = {
method: 'POST',
body: {id: id}
};
fetch("http://localhost:5000/api/Documents/GetNumber", requestOptions)
.then(response => response.json())
.then(result => console.log(result))
.catch(error => { console.log('request failed', error); });
}
and action
[Route("~/api/Documents/GetNumber")]
public string GetNumber( string id)
return id;
}
Upvotes: 4