Skip to content

API Request

Omid Foroqi edited this page Sep 7, 2021 · 4 revisions

API requests format

You can communicate with the NaBot API, with the Post requests to the following URL:

http://localhost:5005/webhooks/rest/webhook

Instead of localhost, you should use your own IP/Address. Also, you can change the 5005 port by changing the following line in the docker-compose file.

    ports:
      - 5005:5005

For example in the 8080 case:

    ports:
      - 5005:8080

Here, in this documentation, you can find some request formats in different languages.

The asked message is: Can you give me dosage information of Abilify?. You can use the message as an argument for your script instead of hard-coding it to the source code.

Bash - cURL

curl --location --request POST 'http://localhost:5005/webhooks/rest/webhook' \
--header 'Content-Type: application/json' \
--data-raw '{
    "message" : "Can you give me dosage information of abilify?",
    "sender" : "default"
}'

Python - Request:

import requests
import json

url = "http://localhost:5005/webhooks/rest/webhook"

payload = json.dumps({
  "message": "Can you give me dosage information of abilify?",
  "sender": "default"
})
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

And then:

python api.py

Python - http.client

import http.client
import json

conn = http.client.HTTPSConnection("localhost", 5005)
payload = json.dumps({
  "message": "Can you give me dosage information of abilify?",
  "sender": "default"
})
headers = {
  'Content-Type': 'application/json'
}
conn.request("POST", "/webhooks/rest/webhook", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

And then:

python api.py

C - libcurl

#include <stdio.h>
#include <curl/curl.h>
 
int main(void)
{
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:5005/webhooks/rest/webhook");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        const char *data = "{\n    \"message\" : \"Can you give me dosage information of abilify?\",\n    \"sender\" : \"default\"\n}";
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    return 0;
}

And then gcc api.c -lcurl

PHP - cURL

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://localhost:5005/webhooks/rest/webhook',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "message" : "Can you give me dosage information of abilify?",
    "sender" : "default"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

NodeJs - Request

var request = require('request');
var options = {
  'method': 'POST',
  'url': 'http://localhost:5005/webhooks/rest/webhook',
  'headers': {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    "message": "Can you give me dosage information of abilify?",
    "sender": "default"
  })

};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

JavaScript - JQuery

var settings = {
  "url": "http://localhost:5005/webhooks/rest/webhook",
  "method": "POST",
  "timeout": 0,
  "headers": {
    "Content-Type": "application/json"
  },
  "data": JSON.stringify({
    "message": "Can you give me dosage information of abilify?",
    "sender": "default"
  }),
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

Clone this wiki locally