[英]Cant pass parameter to API
I'm running a REST API service that has this action:我正在运行具有此操作的 REST API 服务:
[HttpPost]
public FooResponse DoFoo(FooRequest request)
//public FooResponse DoFoo([FromBody] FooRequest request)
{
return null;
}
My request:我的请求:
public class FooRequest
{
public string FooId;
}
I have an Angular client, that's making this call:我有一个 Angular 客户端,正在调用:
startFoo(fooId: string)
{
const url = `${this.baseUrl}StartFoo`;
const params = new HttpParams()
.set('FooId', fooId);
console.log(`params : ${params}`);
const result = this.httpClient.post<fooItem>(url, {params}).toPromise();
return result;
}
When I make the call from PostMan, the FooId is populated, when I call it from Angular, the endpoint is hit, but the param is always null.当我从 PostMan 调用时,FooId 被填充,当我从 Angular 调用它时,端点被命中,但参数始终是 null。 When I look in the console log, the parameters is there.
当我查看控制台日志时,参数就在那里。
I've tried this solution, but it did not resolve my issue.我已经尝试过这个解决方案,但它并没有解决我的问题。
What am I missing?我错过了什么?
You should add [FromBody] attribute in method.您应该在方法中添加 [FromBody] 属性。
[HttpPost]
public FooResponse DoFoo([FromBody] FooRequest request)
{
return null;
}
While you send the request to api, your request body must be in json format.当您将请求发送到 api 时,您的请求正文必须采用 json 格式。
var fooRequest = { FooId : 1};
const result = this.httpClient.post<fooItem>(url, JSON.stringify(fooRequest) ).toPromise();
I did not try, I guess that It will work.我没有尝试,我想它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.