[英]angular http get send body params as json in request
我试图在 angular 中提出这个请求:
curl --location --request GET 'http://localhost:5000/location/searchDistanceAndTags?pageNo=1&size=1' \
--header 'token: $IKFASOmV#*+JIL4X0FGTDZNRPB3GN7HvAB9QD2X8QWeLsqtKW1U5YHnxCMRywEbUZlu??oz!!!!6r' \
--header 'Content-Type: application/json' \
--data-raw '{
"lat": "38.8993487",
"lng": "-77.0145665",
"radius": "100",
"tags": ["bier"]
}'
这是我在 angular 中的代码,不幸的是没有设置参数(后端在 req.body 中没有收到任何数据)
const dataset = {
lat: '38.8993487',
lng: '-77.0145665',
radius: '100',
tags: ['bier']
};
return this._httpClient.get<any>(`http://localhost:5000/location/searchDistanceAndTags?pageNo=${this.pageNumber}&size=10`, {
headers: {token: this.apikey,
'Content-Type': 'application/json'},
params: dataset
});
尽管 GET 请求在技术上可以有一个像这个SO answer中所示的主体,但实际上支持它的库并不多。 正如您所指出的,CURL 确实如此。 但是,像 fetch、Xhr (XmlHttpRequest) 这样的前端 API 不允许在 GET 请求中发送正文。
如果我没记错的话,Angular 的HttpClient
使用 Xhr,所以你不会被允许这样做。
您可以修改 API 以改为接受发布请求,或接受查询字符串中的参数
参数可以通过 HttpParams 发送:
const params = new HttpParams()
.set('lat', 'one!')
.set('lng', "two"); // and so on
可以通过以下方式创建标头:
const headers = new HttpHeaders()
.set("X-CustomHeader", "custom header value");
和
this.httpClient.get<any>(yoururl,{params}) // {params} short form of {params:params}
or:
this.httpClient.get<any>(yoururl,{headers: headers, params: params})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.