简体   繁体   English

Angular HTTP POST:从请求正文创建一个可观察对象

[英]Angular HTTP POST: Creating an observable from the request body

I'm trying to pass a request body to a POST using Angular's HTTPClient.我正在尝试使用 Angular 的 HTTPClient 将请求正文传递给 POST。 From what I can tell, it looks like the body part of the request has to be an observable, but I'm not sure exactly how to go about converting the body of my request to an observable in order to pass it through.据我所知,请求的正文部分似乎必须是可观察的,但我不确定如何将我的请求的正文转换为可观察的以通过它。

Here's the data I'm trying to pass as the body:这是我试图作为正文传递的数据:

options = {
  "response": {
    "mimeType": "application/json"
  },
  "request": {
    "pipeline": [
      {
        "source": {
          "trackEvents": {
            "trackTypeId": "FY9WgbZ-_9QldwEme1VVnb8nhAY",
            "blacklist": "ignore"
          },
          "timeSeries": {
            "period": "dayRange",
            "first": "now()",
            "count": -30
          }
        }
      }
    ]
  }
};

Before this I was just trying to pass this as a regular object:在此之前,我只是想将其作为常规对象传递:

  fetch() {
    this.http.post('https://', {
      headers: new HttpHeaders({
        'X-Integration-Key': '123',
        'Content-Type': 'application/json'
      })
    }, this.options).subscribe(function (data) {
      console.log(data);
    });
  }

(please ignore the URL and integration key here) (请忽略此处的 URL 和集成密钥)

I've found some examples on how people have gone about converting data types to observables but they were with very simple objects and I haven't had any luck with those methods so far.我找到了一些关于人们如何将数据类型转换为 observable 的例子,但它们使用的是非常简单的对象,到目前为止我对这些方法没有任何运气。 Any help would be much appreciated!任何帮助将非常感激!

you've got arguments out of order.你的论据乱了。 body goes second, http options third:正文排在第二位,http 选项排在第三位:

  fetch() {
    this.http.post('https://', this.options, {
      headers: new HttpHeaders({
        'X-Integration-Key': '123',
        'Content-Type': 'application/json'
      })
    }).subscribe(function (data) {
      console.log(data);
    });
  }

the http post function creates an observable. http post 函数创建一个 observable。 it doesn't take an observable as argument.它不以 observable 作为参数。

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

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