[英]how to send FormData and request body in angular httpclient
[英]Angular HttpClient request method how to set body
我需要发送一个带有正文的获取请求。 我使用角 HttpClient。 我知道 get 方法不允许发送正文,所以我正在尝试使用请求方法,但我不明白如何使用它。
我能够从没有正文部分的示例中获取数据,但我确实需要将正文作为 JSON 格式发送。
request(req?: any): any{
const options = createRequestOption(req);
return this.http
.request<ISubscriber[]>("GET", this.resourceUrl,
{
body: '[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
headers: new HttpHeaders({'Content-Type' : 'application/json'}),
params: options,
observe: 'response'
});
}
使用http.get()
只是http.request('GET')
的简写。 如果您确实需要发送 JSON 正文,则必须使用另一种类型的请求 - 例如 post。 像这样的东西可能是你需要的:
return this.http
.post<ISubscriber[]>(
this.resourceUrl,
'[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
{
params: options
{
)
您可能需要更改 API 端点以期待不同的 HTTP 动词。
我听从了你的建议,这是我将来为其他人提供的解决方案......
queryPost(body: string, req?: any) : any {
const options = createRequestOption(req);
return this.http.post<ISubscriber[]>(this.searchUrl, body,
{
headers : new HttpHeaders({"Content-Type": "application/json"}),
params: options,
observe: 'response'
});
}
还提到我必须在我的后端应用程序中创建一个新的 Post 端点。
谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.