简体   繁体   English

CoinMarketCap API 到 Angular 请求

[英]CoinMarketCap API to Angular request

I have my head in water with the new CoinMarketCap API .我对新的CoinMarketCap API 感到困惑

Below is an example of a request in Node.下面是 Node.js 中的请求示例。 How can I make a request in Angular?如何在 Angular 中提出请求? Any suggestions?有什么建议? Thanks.谢谢。

 /* Example in Node.js ES6 using request-promise, concepts should translate 
 to your language of choice */

 const rp = require('request-promise');
 const requestOptions = {
 method: 'GET',
 uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
 qs: {
   start: 1,
   limit: 5000,
   convert: 'USD'
 },
 headers: {
  'X-CMC_PRO_API_KEY': 'API_KEY_HERE'
 },
 json: true,
 gzip: true
};

rp(requestOptions).then(response => {
  console.log('API call response:', response);
}).catch((err) => {
 console.log('API call error:', err.message);
});

As per the documentation found here you cannot perform this HTTP call from a web client:根据此处找到的文档您无法从 Web 客户端执行此 HTTP 调用:

Making HTTP requests on the client side with Javascript is currently prohibited through CORS configuration.当前禁止通过 CORS 配置在客户端使用 Javascript 发出 HTTP 请求。 This is to protect your API Key which should not be visible to users of your application so your API Key is not stolen.这是为了保护您的应用程序用户不应该看到的 API 密钥,以免您的 API 密钥被盗。 Secure your API Key by routing calls through your own backend service.通过您自己的后端服务路由调用来保护您的 API 密钥。

A solution would be to create your own back-end API.一种解决方案是创建您自己的后端 API。 This API can then perform the HTTP call to Coinmarketcap .然后,此 API 可以执行对Coinmarketcap的 HTTP 调用。 Your website then communicates with your custom made back-end API.然后,您的网站与您定制的后端 API 进行通信。

    getAllCoinsListing() {
    const httpOptions = {
      headers: new HttpHeaders({
        'X-CMC_PRO_API_KEY':  'API_KEY_HERE'
      })
    };
    return this.http.get(this.apiUrl, httpOptions);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM