EN How to http request script?

#3
That's not

Example


Code:
registerPlugin({
    name: 'Welcome!',
    version: '1.0',
    description: 'This plugin will let the bot greet everyone.',
    author: 'Michael Friese',
    vars: {
        message: {
            title: 'The message that should be displayed. (%n = nickname)',
            type: 'string'
        }
    }
}, function(sinusbot, config) {
    sinusbot.on('chat', function(ev) {
        if (ev.msg == "!http") {
            $.ajax({
                url: "https://google.com",
                type: 'POST',
                data: example_parameters
            });
        }
    });
});
 
Last edited:

vincolus

Donor
is awesome!
Insider
#5
That's not

Example


Code:
registerPlugin({
    name: 'Welcome!',
    version: '1.0',
    description: 'This plugin will let the bot greet everyone.',
    author: 'Michael Friese',
    vars: {
        message: {
            title: 'The message that should be displayed. (%n = nickname)',
            type: 'string'
        }
    }
}, function(sinusbot, config) {
    sinusbot.on('chat', function(ev) {
        if (ev.msg == "!http") {
            $.ajax({
                url: "https://google.com",
                type: 'POST',
                data: example_parameters
            });
        }
    });
});
this seems to be a jquery function. for that to function, you will have to load the library first
 

vincolus

Donor
is awesome!
Insider
#7
I am posting this here so it does not get lost in case links expire:

Code:
sinusbot.http({
    method:     'POST',
    url:        'http://127.0.0.1:8087/api/v1/bot/login',
    timeout:    6000,
   maxSize:     1024*1024*5,
    headers:    {
        "Content-Type": "application/json",
        "Accept": "application/json"
    },
    body:       '{"username": "user", "password": "pass", "botId": "botId"}'
}, function(error, response) {
    var botToken = JSON.parse(response.data)['token'];
    engine.log('Retrieved token '+botToken);
});
This post was edited on behalf of Xuxes answer.
 
Last edited:

Xuxe

Containerholic
Staff member
V.I.P.
is awesome!
Insider
#8
headers: { "Content-Type": "text/plain", "Accept": "text/plain" }
Better would be application/json - the api only speaks json ;)
Quick Tipp: While using POST,PUT and PATCH (any request with a body of size x) requests you have to set also a Content-Length.
 

vincolus

Donor
is awesome!
Insider
#9
Better would be application/json - the api only speaks json ;)
Quick Tipp: While using POST,PUT and PATCH (any request with a body of size x) requests you have to set also a Content-Length.
That's good to know. Thanks for pointing it out! I edited my first answer.

Is setting content-length really required when also defining maxSize?
 

Xuxe

Containerholic
Staff member
V.I.P.
is awesome!
Insider
#10
That's good to know. Thanks for pointing it out! I edited my first answer.

Is setting content-length really required when also defining maxSize?
Content-Length is definitly related to your body, some server's deny requests when a client send's a body without a Content-Length.

maxSize: 1024*1024*5,
If i am right maxSize is related to the max response size the server can send, remember badboys can send you Gigabytes of response data ;)
 

Schaeffer

Donor
is awesome!
#11
I do not understand where I made a mistake
it returns me error 400: One of the required parameters is missing.
JavaScript:
ar baseUrl = 'https://smsapi.free-mobile.fr/sendmsg';
function smsautomate(msg){
    sinusbot.http({
        method: 'POST',
        url: baseUrl,
       
timeout:    6000,
   maxSize:     1024*1024*5,
    headers:    {
        "Content-Type": "application/json",
        "Accept": "application/json"
    },
        body: '{"user": "secret", "pass": "secret", "msg": "{"name": "test","pseudo": "test","msg": "'+msg+'"}"}'
    },function(error, response) {
        for(var key in response){
            if(typeof response[key] !== 'object'){
                userdebug.chat(key+':'+response[key]);
            }

            if(typeof response[key] === 'object'){
                for(var key0 in response[key]){
                    userdebug.chat(key+':'+key0+':'+response[key][key0]);
                }
            }
        }

    });
}
smsautomate('msg test');
 

Similar threads