以编程方式将 curl 请求主体包含在 edgeSDK 微服务的 http 请求中

Programmatically include curl request body in an edgeSDK microservice's http request

我正在尝试组合一个小的 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.body,并在传递给 context.http.request() 的对象中分配给 data,既作为字符串又作为 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);
        }
    }));
});

你能试试吗:

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

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