javascript - 以编程方式在 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.body
和data
传递给 的对象中的各种排列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);
}
}));
});
解决方案
你能试一下吗:
curl -X POST http://localhost:8083/<BASE_API_PATH>/doStuff
因为它是一个 POST 请求,所以没有显式 HTTP 方法的 curl 可能无法正常工作。
推荐阅读
- c++ - C++ 预处理器扩展类函数名称,就好像它是一个宏一样
- sql - 在 Oracle SQL 中处理时间序列
- forms - 为什么窗体和菜单需要两个窗口?
- javascript - 参考 Gnome-shell 中的“窗口”以用于 Gnome 扩展
- ethereum - 触发非开源智能合约时如何在 etherscan 上对输入数据进行编码?
- python - 如何确定给定的颜色是接近黑色还是接近白色?
- flutter - class 和 typedef 之间的行为不一致
- php - 如何在mysql和php中一次运行多个查询
- android - 如何通过使用 Kotlin 在 android 中编程使设备振动 n 次?
- java - 重启eclipse后重新获得对ViewPart的控制