[英]Angular HttpClient request method how to set body
I need to send a get request with body.我需要发送一个带有正文的获取请求。 Im using angular HttpClient.我使用角 HttpClient。 I understand that get method does not allow to send body, so i'm triyng the request method instead but i cant understand how to use it.我知道 get 方法不允许发送正文,所以我正在尝试使用请求方法,但我不明白如何使用它。
I was able to get data from the exemple bellow without the body part, but i really need to send the body as JSON format.我能够从没有正文部分的示例中获取数据,但我确实需要将正文作为 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'
});
}
Using http.get()
is just a shorthand for http.request('GET')
.使用http.get()
只是http.request('GET')
的简写。 If you really need to send a JSON body, then you'll have to use another type of request - such as post.如果您确实需要发送 JSON 正文,则必须使用另一种类型的请求 - 例如 post。 Something like this might be what you need:像这样的东西可能是你需要的:
return this.http
.post<ISubscriber[]>(
this.resourceUrl,
'[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
{
params: options
{
)
You may need to change your API endpoint to expect a different HTTP verb.您可能需要更改 API 端点以期待不同的 HTTP 动词。
I followed your advice and here is my solution for others in the future...我听从了你的建议,这是我将来为其他人提供的解决方案......
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'
});
}
As also mentione i had to creatre a new Post end point in my back end app.还提到我必须在我的后端应用程序中创建一个新的 Post 端点。
Thank you all谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.