简体   繁体   English

Angular2 http与身体GET?

[英]Angular2 http GET with Body?

I'm querying some elasticsearch servers from my Angular2 site. 我在Angular2网站上查询一些弹性搜索服务器。 To help with security, we'd like to lock down access to only GET requests. 为了帮助提高安全性,我们希望锁定对GET请求的访问权限。 Elasticsearch supports GET with a body but I'm having troubles making it happen with Angular2's http class. Elasticsearch支持GET与一个正文,但我在使用Angular2的http类时遇到麻烦。

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

Since http.get doesn't have a body parameter, I am trying to use the post method. 由于http.get没有body参数,我试图使用post方法。 Previously I would leave off the RequestOptionsArgs of { method: 'GET' } and the POST would go through successfully with the body. 以前我会放弃{ method: 'GET' }的RequestOptionsArgs,POST将成功通过正文。 By specifying the method in the third parameter the http class removes the body from the request. 通过在第三个参数中指定方法,http类将从请求中删除正文。

Is it possible to make a GET request with a body in Angular 2? 是否可以在Angular 2中使用正文发出GET请求?

I think that the raw XHR object doesn't allow this. 我认为原始XHR对象不允许这样做。 To quote the specification (see https://xhr.spec.whatwg.org/ ): 引用规范(参见https://xhr.spec.whatwg.org/ ):

4.5.6 The send() method 4.5.6 send()方法

client . 客户。 send([body = null]) 发送([body = null])

Initiates the request. 发起请求。 The optional argument provides the request body. 可选参数提供请求正文。

The argument is ignored if request method is GET or HEAD. 如果请求方法是GET或HEAD,则忽略该参数。

The send(body) method must run these steps: send(body)方法必须运行以下步骤:

  • If state is not opened, throw an InvalidStateError exception. 如果未打开state,则抛出InvalidStateError异常。

  • If the send() flag is set, throw an InvalidStateError exception. 如果设置了send()标志,则抛出InvalidStateError异常。

  • If the request method is GET or HEAD, set body to null. 如果请求方法是GET或HEAD,则将body设置为null。

This discussion in the Postman github could also help you: https://github.com/postmanlabs/postman-app-support/issues/131 . Postman github中的这个讨论也可以帮助你: https//github.com/postmanlabs/postman-app-support/issues/131

If you want to query an ElasticSearch server, you can use POST requests. 如果要查询ElasticSearch服务器,可以使用POST请求。 Here is a sample: 这是一个示例:

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

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

Hope it helps you, Thierry 希望它对你有帮助,蒂埃里

You might be able to override this by using the generic request method on Http 您可以通过在Http上使用通用request方法来覆盖它

this.http.request(SearchEndpoint, new RequestOptions({
    method: RequestMethod.Get,
    body: q.BuildPayload()
})).subscribe(...);

FWIW, I would be interested in hearing why this is desirable in https://github.com/whatwg/fetch/issues/83 . FWIW,我很想知道为什么这在https://github.com/whatwg/fetch/issues/83中是可取的。 For now there's no browser-based API that supports this, but we could offer it as a feature in fetch() given a convincing enough argument (and implementer interest). 目前还没有基于浏览器的API支持这一点,但我们可以在fetch()提供它作为一个功能,给出足够令人信服的论证(和实现者的兴趣)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM