使用Content-Type时,nodejs https请求引发错误

时间:2019-04-26 21:15:10

标签: javascript node.js api

以下代码与Dropbox API配合使用,在测试过程中可以正常工作,但是当我在破坏的参数中添加“ Content-Type”:“ application / json”时。

https.request(options, function(resp){
        let creds = [];
        let ts = "";
        resp.on('data', function(body){
            creds.push(body);
            let jsonarray = Array.prototype.slice.call(creds, 0).toString().split(",");
            let token = "Bearer " + jsonarray[0].substring(jsonarray[0].indexOf(':') + 3, jsonarray[0].length - 1);
            ts = token;
        });
        resp.on('end', function(){
            let connectfilelist = {
                hostname: 'api.dropboxapi.com',
                path: '/2/files/list_folder',
                method: 'POST',
                headers: {Authorization: ts,
                "Content-Type", "application/json"}
            };

            https.request(connectfilelist, function(resp){
                let flist = [];
                resp.on('data', function(chunk){
                    flist.push(chunk.toString());
                });
                resp.on('end', function(){
                    console.log(flist);
                    res.render('mydropbox', flist);
                });
            }).end(bodyParams);
        });
    }).end(bodyParams);

这是我省略该参数时遇到的错误:

[ 'Error in call to API function "files/list_folder": This function requires its argument in the HTTP request body, but your request body is empty.' ]

当我包含Content-Type时,它会抛出以下内容:

[ 'Error in call to API function "files/list_folder": request body: could not decode input as JSON' ]

我认为我声明参数的方式可能是字符串而不是JSON格式,但是我不确定在哪里更改。

0 个答案:

没有答案