Search code examples
javascriptjsonmicroservicesedgesdkedgeengine

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


I'm trying to put together a small edgeSDK microservice operation that programmatically adds a body to the http request it makes so that the user doesn't have to do it manually as part of the curl command. At the moment, if I execute in a terminal, for example

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

as the API request, it works fine, but if I type

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

and try to have the operation's code programmatically add

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

to the http request object, it has so far just returned a 404 - Not Found error. Is there a way to programmatically add the body, or is including it in the curl command in the terminal the only way to include the body?

Here's (more or less) my code for trying to get this part of the microservice operation to work. I've tried various permutations of assigning the body to request.body, and to data in the object passed to context.http.request(), both as a string and as a JSON object. The most recent iterations are commented out in the code below.

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);
        }
    }));
});

Solution

  • Can you try:

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

    Because it is a POST request, so curl without explicit HTTP method might not work.