Reputation: 667
My angular http request body params are not binding to my api. This is my api which Iam trying to bind body directly without using any complex parameter like class.
public async Task<Object> RejectRequest([FromBody] int RequestId, string
Reason){
}
This is my angular http request:-
rejectRequest(data): any {
var body = JSON.stringify(data);
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
}),
responseType: 'text' as 'text'
};
return this.http.post('/api/Dashboard/RejectRequest',body, httpOptions)
}
my body request is :-
{
"requestid":"45",
"reason":"dfgsdf"
}
Upvotes: 1
Views: 1292
Reputation: 1078
Problem is that, from UI you sending JSON object as body but in API you receiving it as 2 different parameters.
If you are sending parameters using httpParams from ui you can bind it to api using [FromQuery]
So here you can add a new model in API and change your API method as shown below,
public class MyClass
{
public string RequestId { get; set; }
public string Reason { get; set; }
}
public async Task<Object> RejectRequest([FromBody]MyClass MyObj){
//Your code...
}
Upvotes: 3