mysql_udf_http_golang
MySQL or MariaDB UDF(User-Defined Functions) HTTP Client Plugin
Call RESTful API on query.
Setup
- Clone Source
git clone https://github.com/RebirthLee/mysql_udf_http_golang.git udf
cd udf
- Auto Build
bash ./install.sh {username} {password}
{username} replace your MySQL or MariaDB Username.
{password} replace your MySQL or MariaDB Password(Optional).
- Manual Build
bash ./build.sh
Build output is http.so
, move file to plugin_dir
path.
if you don't know plugin_dir
path.
Command input this on MySQL, MariaDB connection.
SHOW VARIABLES LIKE 'plugin_dir';
Ex)
$ mysql -u root -p
Enter password:
And
MariaDB [(none)]> SHOW VARIABLES LIKE 'plugin_dir';
+---------------+-----------------------------------------------+
| Variable_name | Value |
+---------------+-----------------------------------------------+
| plugin_dir | /usr/local/Cellar/mariadb/10.3.12/lib/plugin/ |
+---------------+-----------------------------------------------+
1 row in set (0.001 sec)
and http.so
move to Value
path.
mv ./http.so /usr/local/Cellar/mariadb/10.3.12/lib/plugin/
Finally, execute query
- Http Help
CREATE FUNCTION http_help RETURNS STRING SONAME 'http.so';
- Http Get Method
CREATE FUNCTION http_get RETURNS STRING SONAME 'http.so';
- Http Post Method
CREATE FUNCTION http_post RETURNS STRING SONAME 'http.so';
Usage
- Help
SELECT http_help();
- GET Method
- Prototype
SELECT http_get(url, options...);
- Simple Request
SELECT http_get('http://example.com');
Return
{
"Body" : String(HTML(Default), Base64, Hexdecimal)
}
- Output Option
SELECT http_get('http://example.com', '-O FULL');
Return
{
"Proto" : String(Http Version, HTTP/1.0, HTTP/1.1, HTTP/2.0),
"Status" : String(Status Code, 200 OK, 404 NOT FOUND...),
"Header" : JSON(`{Key : Array, ...}`),
"Body" : String(HTML(Default), Base64, Hexdecimal)
}
-O {outputType}
Define kind of result.
PROTO
, STATUS
or STATUS_CODE
, HEADER
, BODY
(default), FULL
-O PROTO|STATUS|HEADER|BODY
same this -O FULL
.
- Custom Header
SELECT http_get('http://example.com', '-O FULL', '-H CustomKey:CustomValue', '-H Authorization:Bearer a1b2c3d4-123e-5678-9fgh-ijk098765432')
Like this
GET / HTTP/1.1
Host: example.com
CustomKey: CustomValue
Authorization: Bearer a1b2c3d4-123e-5678-9fgh-ijk098765432
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
Option param input -H {key}:{value}
.
- POST Method
- Prototype
SELECT http_post(url, contentType, body, options...)
- Simple Request(No Body)
SELECT http_post('http://example.com', '', '');
- Simple Request(Json Body)
SELECT http_post('http://example.com', 'application/json', '{"Hello":"World"}');
Like this
POST / HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 17
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
{"Hello":"World"}
- Raw Method
- Prototype
SELECT http_raw(method, url, body, options...)
- PUT
SELECT http_raw('PUT', url, body, options...)
- PATCH
SELECT http_raw('PATCH', url, body, options...)
- DELETE
SELECT http_raw('DELETE', url, body, options...)