Angular HttpClient 在 POST 中使用 URL 参数

Angular HttpClient use URL Params in POST

今天我试图发送一个看起来像那样的 post-请求。 http://host/start?time=123456789 使用以下代码:

    const url = `/start`;
    const myParams = new HttpParams().set('time', toTimestamp(time).toString());
    return this.http.post(url, { headers: this.getHttpHeaders(), params: myParams }));

但是发送请求时没有参数 http://host/start。参数在请求正文中发送。 API 不接受,我也不允许更改 API。我如何更改我的代码以将参数包含在 URL?

感谢您的帮助。

当你

return this.http.post(url, { headers: this.getHttpHeaders(), params: myParams }));

您正在请求正文中发送参数,这正是此处所期望的

  1. GET 将 URL 中的数据发送到服务器以从资源
  2. 中获取数据
  3. POST 将正文中的参数发送到服务器 update/creation 资源

在您的请求中遵循此顺序:

 http.post(url, data, httpOptions)

正如 Sanyam 所提到的,post 请求看起来像...

http.post(url, data, httpOptions)

所以在您的请求中您遗漏了实际的 body 部分...因为这是一个 POST 请求。由于您无法控制 API,您可以为 body 部分添加 null

return this.http.post(url, null, { headers: this.getHttpHeaders(), params: myParams }));