首页 > 解决方案 > 如何将 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 新手,我需要帮助。

谢谢!

标签: angulartypescriptcurl

解决方案


我要做的第一件事是弄清楚您提出请求的实际需要。看起来您有一些需要传递的身份验证标头,我假设这是一个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,因此我假设凭据无效。


推荐阅读