Angular HttpClient Get method with body

前端 未结 4 1475
一整个雨季
一整个雨季 2020-12-15 16:57

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

4条回答
  •  抹茶落季
    2020-12-15 17:22

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

    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.

提交回复
热议问题