0

I am trying to use a shell script to save data to mongodb then send the same to elasticsearch for analysis. Below is a psudo code in the shell script that illustrate the issue.

mongo $mongo_conn --eval "

// other javascritp code
db.collection.insertOne(insertData);
http_post_to_elasticsearch(url,insertData);

function http_post_to_elasticsearch(url,insertData){
    // which http_post() method can work here?
    // both fetch and xhr are undefined
    // tried installing npm install -g xmlhttprequest but 'require' is also undefined
}

Below are the functions that have tried but seem not to be supported

async function http_post(url = '', data = {}) {
    print('starting http_post()')
    // Default options are marked with *
    const response = await fetch(url, {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
        mode: 'cors', // no-cors, *cors, same-origin
        cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
        credentials: 'same-origin', // include, *same-origin, omit
        headers: {
            'Content-Type': 'application/json'
            // 'Content-Type': 'application/x-www-form-urlencoded',
        },
        redirect: 'follow', // manual, *follow, error
        referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data) // body data type must match "Content-Type" header
    });
    return response.json(); // parses JSON response into native JavaScript objects
}

async function xhr_post(url = '', data = {}) {
    const xhr = require('xmlhttprequest').XMLHttpRequest;
    // const xhr = new XMLHttpRequest();
    // listen for `load` event
    xhr.onload = async () => {
        // print JSON response
        if (xhr.status >= 200 && xhr.status < 300) {
            // parse JSON
            const response = JSON.parse(xhr.responseText);
            printjson(response);
            // return response;
        }

    };

    // create a JSON object
    const json = data;

    // open request
    xhr.open('POST', url);

    // set `Content-Type` header
    xhr.setRequestHeader('Content-Type', 'application/json');

    // send rquest with JSON payload
    xhr.send(JSON.stringify(json));
}
goremo
  • 43
  • 7
  • `mongo $mongo_conn --eval ".. some javascript commands"` isn't really a shell script and your Q seems to revolve around features found in javascript, yet you haven't included a tag for it. If it is really how to use the shell's `eval` built-in, then you should try to construct a much simpiler test case. Good luck. – shellter Jun 10 '20 at 19:08
  • @shellter, thanks for reminder, i have added javascript in the tags. mongo $mongo_conn --eval ".." is a very simple use case for accessing mongodb shell from bash. everything works ok. the specific question is if fetch() and xmlhttprequest are undefined, how can one send http-post in the scenario above? – goremo Jun 10 '20 at 19:40

1 Answers1

0

mongo shell does not provide a browser nor a nodejs-compatible runtime.

You can look through the server js tests to see if there is anything there that resembles an http client.

See also ReferenceError: require is not defined in MongoDB shell.

D. SM
  • 13,584
  • 3
  • 12
  • 21