How to get html text from a GET


#1

How can I extract the html text returned from a GET?

I have seen lots around about how to retrieve JSON from a GET API eg.

this.http.get('https://www.google.com/randomrestapi').map(res => res.json()).subscribe(data => {
          this.posts = data;
      })

but can’t work out how to get a string with the text of an html response.


#2

If you don’t get any better answers, I would strongly recommend refactoring the overall design to eliminate any need to do this. Retrieving HTML from a backend and trying to inject it into your app is (a) a potential security nightmare, (b) something Angular would prefer you not do, so you will be fighting the framework on many fronts, (c ) a source of frustration if you are trying to use anything outside of really basic formatting like <b> and <i>, leading you to make (a) worse.

If you are writing a scraper, you can do the following:

http.get(url, {responseType: text})

…which will give you something you could pass to an HTML parser.


#3

Thanks for your reply! Yeah I would refactor if possible but i’m hooking into an already existing system and do in fact have to scrape some info from a site. I’ve recommended creating an API but in the meantime, as a stopgap I’m going to scrape.
I’m afraid I might need you to hold my hand a little more… I tried this:


      this.http.get('https://www.mdfcuttosize.com/order/cart.php', {responseType: 'text'}).subscribe(data => {
          this.posts = data;});

and got Runtime error: the selected responseType is not supported!


#4

You are using the Angular 4+ HttpClient, yes? The section entitled “Requesting non-JSON data” in the docs describes this topic.