带有主体的 Angular HttpClient Get 方法 [英] Angular HttpClient Get method with body
问题描述
我正在改进现有的 API,并且要求提供一个可以接受多个搜索条件并根据这些条件执行查询的单一 get 方法.
I'm improving an existing API, and the requirement is to provide a single get method which can accept multiple search criteria and based on those criteria perform the query.
我正在使用 Spring MVC.get 方法签名:
I'm using Spring MVC. The get method signature:
@GetMapping("/subscribers")
public ResponseEntity<List<SubscriberDTO>> getAllSubscribers(Pageable pageable, @RequestBody List<SearchCriteria> lstSearchCriteria)
该实现在 Postman 上按预期工作
The implementation is working as expected tested on Postman
现在我要去 Angular 来实现前端,但我找不到通过 HttpClient Get 方法发送正文的方法...
Now I was going to Angular to implementing the front end and I can't find a way to send a body through HttpClient Get method...
我有点卡住了.我应该通过标题发送搜索条件吗?或者有更好的方法吗?
I'm kind of stuck. Should I send the search criteria over headers? Or there's a better way of doing it?
推荐答案
据我所知不能使用 HttpClient get 来发送正文.您至少可以使用查询使其成为惯用语.(或者你可以尝试以某种方式强迫问题).
As far as I can tell you cannot use HttpClient get to send a body. You could use the query at least for it to be idiomatic. (Or you can try to force the issue somehow).
假设您有一个包含标准的数组:
Lets say you have an array with your criteria:
const criteria = [ {a: 25}, {b: 23} ];
http.get(url + '/?criteria='+ encodeURIComponent( JSON.stringify(criteria)));
在 GET 请求中发送正文违反了某些 RFC 标准,即使它可能有效,您也一定会召唤一些神秘的恶魔.
Sending a body in a GET request is a violation of some RFC standard, and even though it might work you're bound to summon some arcane demons.
这篇关于带有主体的 Angular HttpClient Get 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!