angular - 如何将 cURL 命令转换为 Angular 的 http 请求?
问题描述
我有来自贝宝开发者页面的这个 cURL 命令:
https://developer.paypal.com/docs/api/get-an-access-token-curl/
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
我的问题是,如何将该 cURL 命令转换为带有角度的 http 请求?
你能给我一个例子来说明我该怎么做吗?因为我是 Angular 新手,我需要帮助。
谢谢!
解决方案
我要做的第一件事是弄清楚您提出请求的实际需要。看起来您有一些需要传递的身份验证标头,我假设这是一个POST
请求。
您可以通过使用CURL to Http Request工具将其转换为更熟悉的格式来简化它。
TL; DR:给你:
const headers = new HttpHeaders({
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic Y2xpZW50X2lkOnNlY3JldA=='
});
this.http.post('https://api.sandbox.paypal.com/v1/oauth2/token', 'grant_type=client_credentials', { headers: headers });
this.http
将是您注入的参考HttpClient
这是一个堆栈闪电战。该请求变为 401,因此我假设凭据无效。
推荐阅读
- javascript - 即使在刷新 HTML JQUERY PHP 后如何永久禁用提交输入按钮
- javascript - Firebase auth onCreate 将自定义用户数据添加到实时数据库
- amazon-web-services - 使用 cloudformation 中的标签访问 AWS System Manager Parameter Store
- javascript - 将 Object 转换为 Object Javascript 数组
- vue.js - Vue Composition API 中的反应式
- c - 24 小时到 12:00 时间,进入 12:00 时出现问题,
- android - 如何解决 Android 上的点击/触摸冲突?
- python - 如何解析来自 JSON 请求的数据?
- javascript - 删除隐藏/显示库存工具 GUI 的箭头
- linux - 使用 IOMMU 的 GPU 直通