php - 如何将 "body"参数发送到HttpClient.get()?

标签 php angular httpclient postman angular7

我有一个在 PHP 中创建的“api rest”,该服务返回一个带有“header”、“body”、“get”、“pos”参数的 JSON,它无需任何类型的验证即可接收。
现在我已经创建了一个 angular 服务来连接“api rest”,好吧,我遇到的问题是我想将参数作为“BODY”发送,但我不知道如何,我一直在调查,但我还没有找到一个形状。
是否可以通过 HttpClient.get() 发送“body”?

to test my service use "postman".

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ServicioService {
  constructor(private http: HttpClient) { }
  getQuery(query: string){
    const url = `http://localhost:8080/servicio/`;

    const headers = new HttpHeaders({
      'Authorization': 'Bearer BQAiaibx-we0RSlZFN29B5TPF4t6egxbuuEsc5ZYZhpamHUhImd5'
    });
    const params = new HttpParams()
    .set('page', '2')
    .append('page', '3')
    .set('sort', 'abc');
    return this.http.get (url, { params, headers});
  }
  getNewReleases(){
    return this.getQuery("")
                .pipe( map((data: any) => {
                  return data;
                }));

  }
}

this is what the console prints when executing the code.

最佳答案

GET 请求没有正文。
您应该使用 POST 或 PUT。

您可以阅读 here 一点关于 http 方法的信息。
关于 GET:GET 方法请求指定资源的表示。使用 GET 的请求应仅检索数据,不应有其他影响

因此,发送正文是错误的,因为 GET 方法不应更改任何内容。

关于php - 如何将 "body"参数发送到HttpClient.get()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55618605/

相关文章:

php - 如何测试使用字符串正文发送的 Laravel 邮件,而不是使用 Mailable

javascript - 是否可以让链接在网页源中不可见/隐藏,但在单击按钮时处于事件状态?

javascript - 如何访问发送到 php 脚本的 jQuery.ajax() 返回数据

angular - 新生成的jhipster应用未在产品模式下显示前端(404错误)

spring - Apache HttpClient 向 Spring @Controller 类进行多部分 POST

C# HttpClient 的每个实例都获得它自己的 ServicePoint

linux - LNMP 服务器上的多个 php (php-fpm) 版本

angular - 引用this在Angular2/Typescript中传递回调

javascript - 如何订阅子路由中的参数变化?

angular - ctx。在 Angular 中未定义