Angular

When using Angular you will run into CORS which will prevent you from using resources on other servers than the one hosting your Angular application. Because of this, the example below makes a call to my-host, from where a proxy-server will relay the request to Spirius’s API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class Demo {

  constructor(
    private http: HttpClient) {
  }

  sendSMS(): void {
    const USER = 'Username';
    const PASS = 'Password';
    const RECIPIENT = '+46701234567';
    const SENDER = '+46701234567';
    const MESSAGE = 'Hello world';
    const url = `http://myhost:3000/cgi-bin/sendsms?User=${USER}&Pass=${PASS}&To=${RECIPIENT}&From=${SENDER}&Msg=${MESSAGE}`;
    this.http.get(url, {responseType: 'text'}).subscribe(
      response => {
        // success path
        console.log('Got: ' + response);
      },

      error => {
        switch (error.status) {
          case 400:
            console.log('Bad request.' + error.error);
            break;
          case 401:
            console.log(`Your credentials were rejected with HTTP status code ${error.status}. Check and try again.`);
            break;
          case 409:
            console.log('Rate limit problem: ' + error.error);
            break;
          default:
            console.log('Request failed with HTTP status code: ' + error.status);
            break;
        }
      }
    );
  }
}