gpt4 book ai didi

get - 带有正文的 Angular2 http GET?

转载 作者:太空狗 更新时间:2023-10-29 17:44:04 25 4
gpt4 key购买 nike

我正在从我的 Angular2 站点查询一些 Elasticsearch 服务器。为了帮助提高安全性,我们希望锁定对 GET 请求的访问权限。 Elasticsearch 支持带主体的 GET,但我在使用 Angular2 的 http 类时遇到了麻烦。

this.http.post(SearchEndpoint, q.BuildPayload(), { method: 'GET' })

因为 http.get 没有 body 参数,所以我尝试使用 post 方法。以前我会离开 { method: 'GET' } 的 RequestOptionsArgs 并且 POST 将成功通过正文。通过在第三个参数中指定方法,http 类从请求中删除正文。

是否可以在 Angular 2 中使用主体发出 GET 请求?

最佳答案

我认为原始 XHR 对象不允许这样做。引用规范(参见 https://xhr.spec.whatwg.org/ ):

4.5.6 The send() method

client . send([body = null])

Initiates the request. The optional argument provides the request body.

The argument is ignored if request method is GET or HEAD.

The send(body) method must run these steps:

  • If state is not opened, throw an InvalidStateError exception.

  • If the send() flag is set, throw an InvalidStateError exception.

  • If the request method is GET or HEAD, set body to null.

Postman github 中的这个讨论也可以帮助您:https://github.com/postmanlabs/postman-app-support/issues/131 .

如果要查询 ElasticSearch 服务器,可以使用 POST 请求。这是一个示例:

POST http://localhost:9200/myindex/mytype/_search?pretty=true
Content-Type: application/json

{
"query": {
"match": {
"somefield": "some value"
}
}
}

希望对你有帮助,蒂埃里

关于get - 带有正文的 Angular2 http GET?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35184426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com