2022-08-05 17:05:10 -05:00
| [Home ](/trick ) → [Documentation Home ](../Documentation-Home ) → [Web Server ](Webserver ) → [APIs ](WebServerAPIs ) → Extending the HTTP API |
|------------------------------------------------------------------|
2021-08-18 13:15:10 -05:00
## Extending the HTTP-API
The HTTP-API is implemented as a collection of ```httpMethodHandlers` ``. An ` ``httpMethodHandler` `` is a pointer to a function that is expected to respond to an HTTP GET request, using the **CivetWeb** framework. An ` ``httpMethodHandler` `` is defined (in ` ``trick/CivetWeb.hh` ``) as follows:
```c
typedef void (*httpMethodHandler)(struct mg_connection *, void* cbdata);
```
Documentation for the **CivetWeb Networking Library** can be found at:
[https://cesanta.com/docs/overview/intro.html ](http://civetweb.github.io/civetweb/ )
## Example HTTP-API Extension
Suppose you want your web server to send you a JSON message:
```json
{ "greeting" : "Hello Trick Sim Developer!" }
```
when you invoke the URL: ```http://localhost:8888/api/http/hello` ``.
### Creating an ```httpMethodHandler```.
The following two files will be our implementation of an ```httpMethodHandler` ``. We'll put these in some models directory ` ``httpMethods/` ``.
**```handle_HTTP_GET_hello.h` ``**
2019-11-19 14:21:07 -06:00
```c
#ifndef HANDLE_HTTP_GET_HELLO
#define HANDLE_HTTP_GET_HELLO
#ifndef SWIG
2021-08-18 13:15:10 -05:00
void handle_HTTP_GET_hello(struct mg_connection *nc, void *hm);
2019-11-19 14:21:07 -06:00
#endif
2021-08-18 13:15:10 -05:00
#endif
2019-11-19 14:21:07 -06:00
```
2021-08-18 13:15:10 -05:00
**```handle_HTTP_GET_hello.c` ``**
```c
2021-10-19 13:41:42 -05:00
#include "CivetServer.h"
2021-10-19 14:26:06 -05:00
#include "civetweb.h"
2021-08-18 13:15:10 -05:00
#include <string.h>
void handle_HTTP_GET_hello(struct mg_connection *nc, void *hm) {
2019-11-19 14:21:07 -06:00
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
const char* json_text =
"{ \"greeting\" : \"Hello Trick Sim Developer!\" }";
2021-08-18 13:15:10 -05:00
mg_send_chunk(nc, json_text, strlen(json_text));
mg_send_chunk(nc, "", 0);
}
```
### Installing our ```httpMethodHandler```.
We'll do this from our **S_define** file:
* Add ```(httpMethods/handle_HTTP_GET_hello.c)` `` to the ` ``LIBRARY DEPENDENCIES` ``.
* Include our header file:
```##include "httpMethods/handle_HTTP_GET_hello.h"` ``
* In ```create_connections()` `` add :
```c
web.server.installHTTPGEThandler( "hello", & handle_HTTP_GET_hello );
```
### A Complete S_define
```c++
2019-11-19 14:21:07 -06:00
/***********************TRICK HEADER*************************
PURPOSE:
(Cannon Numeric)
LIBRARY DEPENDENCIES:
(
(cannon/gravity/src/cannon_init.c)
(cannon/gravity/src/cannon_numeric.c)
(httpMethods/handle_HTTP_GET_hello.c)
)
*************************************************************/
#include "sim_objects/default_trick_sys.sm"
2021-08-18 13:15:10 -05:00
#include "sim_objects/CivetServer.sm"
2019-11-19 14:21:07 -06:00
##include "cannon/gravity/include/cannon_numeric.h"
##include "httpMethods/handle_HTTP_GET_hello.h"
class CannonSimObject : public Trick::SimObject {
public:
CANNON cannon ;
int foo;
CannonSimObject() {
("default_data") cannon_default_data( & cannon ) ;
("initialization") cannon_init( & cannon ) ;
("derivative") cannon_deriv( & cannon ) ;
("integration") trick_ret = cannon_integ( & cannon ) ;
("dynamic_event") cannon_impact( & cannon) ;
}
} ;
CannonSimObject dyn ;
IntegLoop dyn_integloop (0.01) dyn;
void create_connections() {
dyn_integloop.getIntegrator(Runge_Kutta_4, 5);
web.server.installHTTPGEThandler( "hello", & handle_HTTP_GET_hello );
}
2021-08-18 13:15:10 -05:00
```
2022-08-05 17:05:10 -05:00
Continue to [Extending the WS API ](Extend-ws-api )