• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

API (not) a bug (POST)

Status
Not open for further replies.

SUNSHINE

New Member
Hi,

I have a bug with the API.
I try this script :
Code:
    $sURL = "[URL]http://x:8088/api/v1/bot/login[/URL]"; // The POST URL
    $sPD = "username=admin?password=foobar?botId=1"; // The POST Data
    $aHTTP = array(
     'http' => // The wrapper to be used
        array(
        'method'  => 'POST', // Request Method
        // Request Headers Below
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $sPD
     )
    );
    $context = stream_context_create($aHTTP);
    $contents = file_get_contents($sURL, false, $context);

    echo $contents;
He result to an error :
{"success":false,"code":406,"error":"invalid character 'u' looking for beginning of value"}

How to fix ?
Thanks
 

Xuxe

Containerholic
Staff member
is awesome!
V.I.P.
Contributor
Insider
He result to an error :
{"success":false,"code":406,"error":"invalid character 'u' looking for beginning of value"}

How to fix ?
Thanks

Read the Documentation, the whole API want's JSON.
You need a other Content Type: application/json
And you need to encode your data as json.
The bot ID is never 1, the Bot ID is a hash (You get it from /api/v1/botId)

https://www.sinusbot.com/api/
 

mxschmitt

Moderator
Staff member
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
for JSON examples, just open the developement tools of google chrome, firefox etc, and check the network traffic
 

Xuxe

Containerholic
Staff member
is awesome!
V.I.P.
Contributor
Insider
PHP:
Code:
$myJson = "{\"mystring\": \"Hello World.\"}";
$array = json_decode($myJson, true);

var_dump($array);
echo json_encode($array);
 

Xuxe

Containerholic
Staff member
is awesome!
V.I.P.
Contributor
Insider
Can you send me script/screen ? I don't find that.

Ask google, keyword your programming language + JSON. This should not be so hard.
Also there are tons of JSON Validators ;)
 

SUNSHINE

New Member
Can you give me an example with SINUSBOT ? Because actually I don't understand your script, Idk JSON script.

Where I put link ? Parameters ? ..

Very thanks for your help
 

Xuxe

Containerholic
Staff member
is awesome!
V.I.P.
Contributor
Insider
Where I put link ? Parameters ? ..

I linked you the Documentation, the JSON must be sent in the Body. This is basic Programming Knowledge.
I don't know which language you use so i can't help. How i said use google, if you know how it works basicly in your language you know also how it works with the Sinusbot.
 

SUNSHINE

New Member
Okay, I have maked this :

Code:
$data = array("username" => "admin", "password" => "foobar", "botId" => "c471f075-c4bc-4f73-8127-20e1a30c98ea");                                                                   
$data_string = json_encode($data);                                                                                 
                                                                                                                    
$ch = curl_init('http://x:8088/api/v1/bot/login');                                                                     
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                     
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                         
    'Content-Type: application/json',                                                                               
    'Content-Length: ' . strlen($data_string))                                                                     
);                                                                                                                 
                                                                                                                    
$result = curl_exec($ch);
print_r($result);

He return :

What is this "token" ?
 

Xuxe

Containerholic
Staff member
is awesome!
V.I.P.
Contributor
Insider
What is this "token" ?

I removed the Token, this is your login Token. Required for all other calls.
You need to add this token as Header -> Authorization: bearer YOURTOKENHERE TO ALL OTHER CALLS THEN LOGIN.
 

SUNSHINE

New Member
New error :
Code:
$data = array("name" => "test", "password" => "testpass", "tsuid" => "test", "tsgid" => 60, "privileges" => 5);                                                                    
$data_string = json_encode($data);                                                                                  
$token_id = "x";                    
                   
$ch = curl_init('http://x:8088/api/v1/bot/users');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                    
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                      
    "Authorization: bearer $token_id",                                                                              
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                      
);                                                                                                                  
                                                                                                                     
$result = curl_exec($ch);
print_r($result);

Return : {"success":false,"code":8081,"error":"'name' or 'password' not set"}

But I set "name" and "password" in the array "$data"...
 

SUNSHINE

New Member
Done.

Code:
$data = array("name" => "test", "password" => "testpass", "privileges" => 2147483647);

Same error : {"success":false,"code":8081,"error":"'name' or 'password' not set"}
 
Status
Not open for further replies.
Top