json - 需要帮助使用 Angular 4 发送此 curl 请求

标签 json angular http curl

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

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

最佳答案

首先你必须在你的 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);
    }       
}

然后您可以从应用程序的任何位置调用 sendPostRequest()

关于json - 需要帮助使用 Angular 4 发送此 curl 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47380778/

相关文章:

json - Eclipse:使用 Json 编辑器插件自动格式化 JSON 文件,例如 AWS Cloudformation Template

angularjs - Angular 2 中接口(interface)的命名约定是什么?

java - 读取HTTP InputStream的麻烦

java - 在 android 中读取 XML 响应

json - 处理包含多种类型的 JSON 数组 - Swift 4 Decodable

javascript - 解析包含带函数的序列化对象的 HTTP 响应

css - Angular:ngFor 中不可变模型的动画

angular - 语义 UI - Accordion 未在 Angular 中扩展

ruby - Rails 对象生命周期 - 跨事务对象创建和销毁

javascript - 如何使用 JQuery 使用循环插入 JSON 数据中的图像?