title: Angular 7 - how to send HTTP call before browser close
body: <p>I am trying to send a POST HTTP request before browser close. </p>
  
  <p>Basically I'm trying to event out the user action that they've closed the page, but the call isn't making it out of the app before it shuts down. </p>
  
  <p>I am under the impression that this isn't possible with a normal HTTP request because the fact you must subscribe to the HTTP request. </p>
  
  <p>I've tried the following solution found here: <a href="https://stackoverflow.com/questions/38999842/angular-2-execute-code-when-closing-window/39005933">Angular 2 - Execute code when closing window</a></p>
  
  <p>This won't work for me as I'm not doing simple GET. Here's what I've got so far. </p>
  
  <p><strong>component.ts</strong></p>
  
  <pre><code>@HostListener('window:beforeunload', ['$event'])
    beforeunloadHandler($event) {
      this.eventsService.sendEvent(this.form.value).subscribe(() =&gt; {
        console.log("Made it");
      });
    }
  </code></pre>
  
  <p><strong>service.ts</strong></p>
  
  <pre><code>sendEvent(userData): Observable&lt;any&gt; {
      return this.http.post&lt;any&gt;(this.url, userData,
        {
          headers: new HttpHeaders({
            "Content-Type": "application/json"
          })
        })
    }
  </code></pre>
  
  <p>Is there a way to utilize XMLHttpRequest() like the solution above states? What other options are there for a solution like this?</p>
  
  <p>Any help is appreciated. </p>