Shaharyar Kirmani
Shaharyar Kirmani

Reputation: 305

Need help to send this curl request using angular 4

I have this curl command

curl -X POST \
https://www.wellingtonsoccer.com/lib/api/auth.cfc?returnFormat=JSON&method=Authenticate' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: b408a67d-5f78-54fc-2fb7-00f6e9cefbd1' \
-d '{"email":"[email protected]",
"user_password":"mypasss",
"token":"my token"}

I want to send http post in angular 4 that is same as this curl request.

Upvotes: 3

Views: 14387

Answers (1)

Michele Da Rin
Michele Da Rin

Reputation: 287

First you have to import the HttpClient module in your app.module file import { HttpClientModule } from '@angular/common/http, then you can build a service (recommended) that should be something like this:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable()
export class MyService () {
    url: string = 'https://www.wellingtonsoccer.com/lib/api/auth.cfc?returnFormat=JSON&method=Authenticate';

    constructor (private http: HttpClient) { }

    sendPostRequest() {
         const headers = new HttpHeaders()
             .set('cache-control', 'no-cache')
             .set('content-type', 'application/json')
             .set('postman-token', 'b408a67d-5f78-54fc-2fb7-00f6e9cefbd1');

         const body = {
             email: '[email protected]',
             user_password: 'mypasss',
             token: 'my token'
         }

         return this.http
                    .post(this.url, body, { headers: headers })
                    .subscribe(res => res.json);
    }       
}

Then you can call sendPostRequest() from anywhere in your app.

Upvotes: 11

Related Questions