Please see my c# core api code as following:
[HttpPost]
public bool Save([FromBody] string data,[FromQuery] ICollection<int> list)
{
var result = true;
return result;
}
and angular post call as following:
postAction(data: any, list: number[]): Observable<any> {
let params = new HttpParams().set('scopes', JSON.stringify(list));
return this.httpClient.post( `${this.apiUri}/Save`,data, { params });
}
with these i'm getting an "One or more validation errors occurred" error with status code 400.
can't we use FromBody and FromQuery at same time ?
Update 1:
Code Details 400 Undocumented Error: Response body
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-b6e1d156b5ff1940a6186060c4902663-36e8ff61a6d6374b-00",
"errors": {
"$": [
"'s' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."
]
}
}
The code does not reach the controller because ASP.NET Core runs a validation to assert that the data in the request are valid and can be mapped to the parameters of your action. When sending data to ASP.NET Core, it is important that the structure of the data meets the expectations of the action, if not, the framework returns a BadRequest response (status code 400).
In your code, the query parameter is named
list, in the Angular code, you send it asscopes. These should match, so you should rename one of them (or use the constructor parameter ofFromQueryto set the name toscopes).In addition, remove the
JSON.stringifyfor the query parameter and just assign the array. The url will then have several values forscopes, e.g.:ASP.NET Core Web API parses the URI in a different way and will assign the values to the
scopesparameter.