Angular HttpClient请求方法如何设置正文

Bru*_*uel 7 angular angular-httpclient

我需要发送带有正文的获取请求。我使用的是 Angular 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'
        });
}
Run Code Online (Sandbox Code Playgroud)

小智 5

usinghttp.get()只是 的简写http.request('GET')。如果您确实需要发送 JSON 正文,那么您必须使用另一种类型的请求 - 例如 post。您可能需要这样的东西:

return this.http
  .post<ISubscriber[]>(
    this.resourceUrl,
    '[{"key": "phoneLineType", "operation": ">", "value": "200"}]',
    {
      params: options
    {
  )
Run Code Online (Sandbox Code Playgroud)

您可能需要更改 API 端点以期待不同的 HTTP 动词。