【问题标题】:VSCode Extension REST calls not workingVSCode 扩展 REST 调用不起作用
【发布时间】:2018-06-19 17:45:14
【问题描述】:

我正在尝试为 Visual Studio Code 构建一个扩展,但我似乎遇到了从扩展内部发出 POST 请求的问题。我创建了一个脚本,它提交一个多部分表单以将文件上传到本地运行的服务器。我已经通过 Postman 测试了 API,它工作正常。我实际上可以使用 Node 从命令提示符窗口运行脚本,它可以很好地提交请求。但是,作为 VSCode 扩展的一部分运行的完全相同的脚本(在调试模式下)在某些情况下根本无法发出请求,或者似乎无法正确提交表单。

这是脚本中的代码

//Sync a package to the AEM server
const PACKAGEMGR_PATH = "/crx/packmgr/service.jsp";
const fs = require('fs');
var rp = require('request-promise');
rp.debug = true;
const parseUrl = require('url').parse

class Sync {
    syncPackage(packagePath, host, port, username, password) {
        var url = parseUrl("http://" + username + ":" + password + "@" + host + ":" + port + PACKAGEMGR_PATH);
        var auth = Buffer.from(url.auth).toString('base64');
        var cleanURL = "http://" + url.host + url.path;
        console.log('Basic ' + auth);
        console.log(cleanURL);
        return new Promise((resolve, reject) => {
            rp({uri: "http://localhost:3000", formData: {
                "file": fs.createReadStream(packagePath),
                "force": "true",
                "install": "true",
                "name": "file"
            }, method: "POST", headers: {'Authorization': 'Basic ' + auth}}).then((resp) => {
                console.log(resp);
            }).catch((err) => {
                console.error(err);
            })
        });
    }
}

module.exports = new Sync();

现在有了上面的例子,请求将发送到服务器,但服务器无法理解表单数据,就像我发送了一个空表单一样。

所以为了测试发送了什么数据,我在 localhost:3000 上设置了一个本地快速回显服务器。同样,从命令行运行上面的脚本运行良好,我的回显服务器能够读取表单并将其吐回给我。

但是,在 VSCode 扩展调试模式下运行时,尝试发布表单时出现此错误: RequestError: 错误:写入 ECONNRESET

那么,为了让 VSCode 扩展的 HTTP 请求正常工作,我需要做些什么特别的事情吗?

【问题讨论】:

    标签: node.js visual-studio-code vscode-extensions


    【解决方案1】:

    据我所知,您在代码中有一些语法问题,请仔细检查文档。

    将 request-promise 与 json 数据一起使用

    var options = {
        method: 'POST',
        uri: 'http://api.posttestserver.com/post',
        body: {
            some: 'payload'
        },
        json: true // Automatically stringifies the body to JSON
    };
    
    rp(options)
        .then(function (parsedBody) {
            // POST succeeded...
        })
        .catch(function (err) {
            // POST failed...
        });
    

    但是如果您想将请求作为表单数据发送,请使用这种格式

    var options = {
        method: 'POST',
        uri: 'http://posttestserver.com/post.php',
        form: {
            // Like <input type="text" name="name">
            name: 'Josh'
        },
        headers: {
            /* 'content-type': 'application/x-www-form-urlencoded' */ // Is set automatically
        }
    };
    

    【讨论】:

    • 我没有使用 JSON,而且“form”也不适用于我正在使用的 multipart/form-data。同样,此脚本在命令提示符中的 Node 中运行 100% 良好。该问题仅在作为 Visual Studio Code 扩展的一部分运行时出现。
    【解决方案2】:

    我遇到了同样的问题,并且能够确认这只是 VSCode 扩展的问题 --- 浏览器和其他请求正常成功。

    转到您的快速服务器并将“localhost”添加到此行:

    app.listen(PORT, () => {console.log(`Server running on port ${PORT}`)})
    

    像这样:

    const express = require('express')
    const app = express()
    
    const PORT = 3001
    app.listen(PORT, 'localhost', () => {
      console.log(`Server running on port ${PORT}`)
    })
    

    【讨论】:

      猜你喜欢
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多