简体   繁体   中英

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

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. Any help would be much appreciated!

you've got arguments out of order. body goes second, http options third:

  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. it doesn't take an observable as argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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