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.

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;
        }
      }
    );
  }
}