首页 > 解决方案 > 以编程方式在 edgeSDK 微服务的 http 请求中包含 curl 请求正文

问题描述

我正在尝试组合一个小的 edgeSDK 微服务操作,该操作以编程方式将正文添加到它发出的 http 请求中,这样用户就不必在 curl 命令中手动执行此操作。目前,如果我在终端中执行,例如

curl -d '{"jsonrpc": "2.0", "method": "getMe", "params": [""]}' http://localhost:8083/<BASE_API_PATH>/doStuff

作为 API 请求,它工作正常,但如果我输入

curl http://localhost:8083/<BASE_API_PATH>/doStuff

并尝试以编程方式添加操作代码

{"jsonrpc": "2.0", "method": "getMe", "params": [""]}

对于 http 请求对象,到目前为止它只返回了404 - Not Found错误。有没有办法以编程方式添加正文,或者将其包含在终端的 curl 命令中是包含正文的唯一方法?

这是(或多或少)我试图让这部分微服务操作工作的代码。我尝试了将主体分配给request.bodydata传递给 的对象中的各种排列context.http.request(),既作为字符串也作为 JSON 对象。最近的迭代在下面的代码中被注释掉了。

app.post('/doStuff', (request, response) => 
{
    //const body = JSON.stringify(JSON.parse('{"jsonrpc": "2.0", "method": "getMe", "params": [""]}'));
    //const body = JSON.stringify({"jsonrpc": "2.0", "method": "getMe", "params": [""]});
    //const body = '{"jsonrpc": "2.0", "method": "getMe", "params": [""]}';
    //const body = {"jsonrpc": "2.0", "method": "getMe", "params": [""]};

    //request.body = body;

    context.http.request((
    {
        type: 'POST',
        //data: body,
        url: 'http://localhost:8083/jsonrpc/v1',
        success: function(r) 
        {
            //TODO: stuff

            response.end('Finally, it works');
        },
        error: function(err) 
        {
            response.end(err.message);
        }
    }));
});

标签: javascriptjsonmicroservicesedgesdkedgeengine

解决方案


你能试一下吗:

    curl -X POST http://localhost:8083/<BASE_API_PATH>/doStuff

因为它是一个 POST 请求,所以没有显式 HTTP 方法的 curl 可能无法正常工作。


推荐阅读