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