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

Sha*_*ani 3 json curl http angular

我有这个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"}
Run Code Online (Sandbox Code Playgroud)

我想以与该curl请求相同的角度4发送http发布。

小智 5

首先,您必须在app.module文件中导入HttpClient模块import { HttpClientModule } from '@angular/common/http,然后可以构建一个类似于以下内容的服务(推荐):

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);
    }       
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以sendPostRequest()从应用程序中的任何位置拨打电话。