Send a POST request using Node.js

284 views Asked by At

I'd think this should be fairly easy using one of the modules off NPM, but I've tried two different ones and they both send the URL without the tags appended as far as a I can tell.

The url is: https://safebooru.org/index.php?page=dapi&s=post&q=index and what needs to be sent is a pid, limit, and tags.

However what I keep getting back is results as if I had sent just 'https://safebooru.org/index.php?page=dapi&s=post&q=index'

instead of say

'https://safebooru.org/index.php?page=dapi&s=post&q=index&pid=1&limit=10&tags=brown_hair'

Please. Is there a module out there that will send this dang request as intended and not just the base URL provided?

The modules I tried were 'request' and 'superagent', which I was led to via similar questions on stackoverflow.

const rp = require("request")
const sa = require("superagent");

class SafebooruGetter {
    constructor(data){
        //none
    }

    get(limit, page, tags, callback){
        var results;
        sa.post('https://safebooru.org/index.php?page=dapi&s=post&q=index')
        .send({limit: limit, pid: page, tags: tags})
        .end(function(err, res){
            if(err)
                console.log(err);
            else
                callback(res);
        });

    }


    get2(limit, page, tags){
        var options = {
            method: 'POST',
            url: 'https://safebooru.org/index.php?page=dapi&s=post&q=index',
            form: {
                "limit": limit,
                "pid": page,
                "tags": tags,
            },
            headers: {
                'User-Agent': 'Super Agent/0.0.1',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
            //json: true
        };
        //console.log(rp(options));
        // return rp(options).then((data) => { return (data)});
        return rp(options, function(error, response, body){
            if(!error && response.statusCode == 200){
                console.log(body);
                return body;
            }
        });
    }
}
1

There are 1 answers

0
Akshay Kumar On BEST ANSWER

You are sending the parameters as form data in this,

form: {
            "limit": limit,
            "pid": page,
            "tags": tags,
        },

But you are expecting it to come as a query parameter like this url,

 https://safebooru.org/index.php?page=dapi&s=post&q=index&pid=1&limit=10&tags=brown_hair

Thats not possible.

If you want it to be sent as query parameter only then send like this,

get2(limit, page, tags){
    var options = {
        method: 'POST',
        url: 'https://safebooru.org/index.php?page=dapi&s=post&q=index&pid='+page+'&limit='+limit+'&tags='+tags,
        headers: {
            'User-Agent': 'Super Agent/0.0.1',
            'Content-Type': 'application/x-www-form-urlencoded'
        }
        //json: true
    };
    //console.log(rp(options));
    // return rp(options).then((data) => { return (data)});
    return rp(options, function(error, response, body){
        if(!error && response.statusCode == 200){
            console.log(body);
            return body;
        }
    });
}

On the other side catch it as query parameters only like in node,

  var limit = req.query.limit
  var pid = req.query.pid
  var tags = req.query.tags

Hope this helps.