【问题标题】:Need help to send this curl request using angular 4需要帮助使用 Angular 4 发送此 curl 请求
【发布时间】:2018-05-03 00:12:56
【问题描述】:

我有这个 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"}

我想以与此 curl 请求相同的角度 4 发送 http 帖子。

【问题讨论】:

    标签: json angular http curl


    【解决方案1】:

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

    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()

    【讨论】:

      猜你喜欢
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      相关资源
      最近更新 更多