需要帮助使用角度4发送此卷曲请求

时间:2017-11-19 18:59:12

标签: json angular http curl

我有这个卷曲命令

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":"myemail@xyz.com",
"user_password":"mypasss",
"token":"my token"}

我想在角度4中发送与此卷曲请求相同的http帖子。

1 个答案:

答案 0 :(得分:3)

首先,您必须在app.module import { HttpClientModule } from '@angular/common/http中导入HttpClien,然后您可以构建一个应该是这样的服务(推荐):

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: 'myemail@xyz.com',
             user_password: 'mypasss',
             token: 'my token'
         }

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

然后,您可以在应用中的任何位置拨打sendPostRequest()