【问题标题】:How to send the "body" parameter to HttpClient.get ()?如何将“body”参数发送给HttpClient.get()?
【发布时间】:2019-04-10 17:51:13
【问题描述】:

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

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;
                }));

  }
}

【问题讨论】:

  • 如果您计划将数据发送到 REST API,您应该使用 POST 请求而不是 GET 请求

标签: php angular httpclient postman angular7


【解决方案1】:

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

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

因此,发送正文是错误的,因为 GET 方法不应该改变任何东西。

【讨论】:

  • 拥有body并不意味着后端发生变化
  • So, it would be wrong to send a body because a GET method should not change anything. 你的意思是,你想要哪一页的结果?还是过滤器?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 2022-01-15
  • 2016-08-20
  • 1970-01-01
  • 2022-12-03
  • 2018-08-27
  • 2023-01-02
相关资源
最近更新 更多