bobor
bobor

Reputation: 9

How to pass arguments in express.js

kind of a noob question, hopefully someone can help me with.

I am making an application which uses express.js to communicate with bitcoin-core by sending http reqests.

router.get("/getbalance", (req, res) => {
var dataString = `{"jsonrpc":"1.0","id":"curltext","method":"getbalance","params":[]}`;
var options = {
    url: `http://${USER}:${PASS}@127.0.0.1:18443/`,
    method: "POST",
    headers: headers,
    body: dataString
};

callback = (error, response, body) => {
    if (!error && response.statusCode == 200) {
        const data = JSON.parse(body);
        res.send(data);
    }
};
request(options, callback);

So far it is working quite well, and the problem arises when i try to pass more than one argument to the request, e.g., when i try to send bitcoin to an address in the console, the command is:

bitcoin-cli -regtest sendtoaddress [someaddress] [amount]

i tried to do it like this:

http://localhost:4444/api/sendtoaddress/bcrt1q3nczv7jr88rwvhsyv2rx49l3czfxurzfk240ue/10

but it just freezes.

This is what the "sendtoaddress" request looks like so far.

router.get("/sendtoaddress/:addr/:amount", (req, res) => {
var dataString = `{"jsonrpc": "1.0", "id": "curltext", "method": "sendtoaddress", "params": [${req.params.addr}, ${req.params.amount}]}`
console.log(req.params.addr)
var options = {
    url: `http://${USER}:${PASS}@127.0.0.1:18443/`,
    method: "POST",
    headers: headers,
    body: dataString
};
callback = (error, response, body) => {
    if (!error && response.statusCode == 200) {
        const data = JSON.parse(body);
        res.send(data);
    }
};
request(options, callback);

Upvotes: 0

Views: 121

Answers (1)

user20182014
user20182014

Reputation:

As I'm a new contributor so can't create a comment. it would be better if u could use query method to get the data from the url.

A basic get url would look like this:

"https://xyc.com/vehicle?name="abc"&color="color"

query method is used to get the fields after the ?

We use req.params to get the :id from the url.

you can try changing your get url from this:

"/sendtoaddress/:addr/:amount"

to this:

"/sendtoaddress/:addr?amount="some_amount"

I hope this helps.

Upvotes: 0

Related Questions