【问题标题】:pdflayer API post request saying I didnt not provide api_key even though it didpdflayer API 发布请求说我没有提供 api 密钥,即使它提供了
【发布时间】:2018-04-28 01:19:54
【问题描述】:

我目前正在尝试使用来自http://www.pdflayer.com 的 API,但是我在通过 Axios 的发布请求提供 api_key 时遇到了问题。

我的代码如下所示:

var config = require('./../config');
var axios = require('axios');
var fs = require('fs');

const BASE_URL = 'http://api.pdflayer.com/api/convert';

module.exports = {

    createQuotePdf() {
        var data = {
            document_url: 'https://www.apple.com',
            access_key: config.pdflayer_acccess_key,
            page_size: 'A4',
            test: '1'
         }

        axios.post(BASE_URL, data)
        .then((data) => {
            console.log(data);
            fs.writeFile('./download.pdf', data.body, function(err) {
                if (err) console.log('error: ', err);
            })
        });
    }

};

但是,每次我提出请求时,都说我没有提供 api 密钥,即使我指定了。

如果有人可以帮助我,那就太好了。

最好的问候

【问题讨论】:

    标签: javascript api post axios


    【解决方案1】:

    您是否尝试过将其直接附加到您的 BASE_URL,因为接缝是他们构建它的方式

    BASE_URL = ('http://api.pdflayer.com/api/convert?access_key=', YOUR_ACCES_KEY)'

    【讨论】:

    • 是的,我试过了,这也有效,但是在这种情况下,我通过发布请求提供的所有其他参数也不会被检测到,所以即使我的 api 密钥可以工作,我仍然有其他参数也有同样的问题。这就是为什么我认为这是同一个根本问题。并且一些参数需要通过 Post 提供,而不是附加到 URL 中。
    • 一个只能通过 post 访问的参数示例是 document_html
    • 我明白了,看看能不能找到解决办法。
    【解决方案2】:

    对于遇到相同问题的任何人,以下是可行的解决方案:

    var config = require('./../config');
    var request = require('request');
    
    var BASE_URL = 'http://api.pdflayer.com/api/convert';
    var ACCESS_KEY = '?access_key=' + config.pdflayer_acccess_key;
    var API_URL = BASE_URL + ACCESS_KEY;
    
    module.exports = {
    
        createPdf() {
    
            var formData = {
                document_html: `<html><body>Hello World</body></html>`
            }
    
            request.post({url: API_URL, formData: formData, encoding: null}, function optionalCallback(err, httpResponse, body) {
                if (err) {
                    console.log(err);
                } else {
                  // Here you can save the file or do anything else with it
                    console.log(body);
                }
            });
        }
    
    };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-20
      • 2019-04-30
      • 2023-02-22
      • 2020-01-31
      • 2017-10-25
      • 2021-05-25
      • 1970-01-01
      • 2020-10-22
      相关资源
      最近更新 更多