Skip to content

Commit 5ea01c9

Browse files
committed
Merge branch 'lucajoos-master'
2 parents 4f17e83 + 488d5c1 commit 5ea01c9

12 files changed

+95
-47
lines changed

META.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "http",
33
"abstract": "HTTP client for PostgreSQL",
44
"description": "HTTP allows you to get the content of a web page in a SQL function call.",
5-
"version": "1.4.0",
5+
"version": "1.5.0",
66
"maintainer": [
77
"Paul Ramsey <[email protected]>"
88
],
@@ -21,9 +21,9 @@
2121
},
2222
"provides": {
2323
"http": {
24-
"file": "http--1.4.sql",
24+
"file": "http--1.5.sql",
2525
"docfile": "README.md",
26-
"version": "1.4.0",
26+
"version": "1.5.0",
2727
"abstract": "HTTP client for PostgreSQL"
2828
}
2929
},

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ As seen in the examples, you can unspool the array of `http_header` tuples into
232232
* `http_post(uri VARCHAR, data JSONB)` returns `http_response`
233233
* `http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR)` returns `http_response`
234234
* `http_patch(uri VARCHAR, content VARCHAR, content_type VARCHAR)` returns `http_response`
235-
* `http_delete(uri VARCHAR)` returns `http_response`
235+
* `http_delete(uri VARCHAR, content VARCHAR, content_type VARCHAR))` returns `http_response`
236236
* `http_head(uri VARCHAR)` returns `http_response`
237237
* `http_set_curlopt(curlopt VARCHAR, value varchar)` returns `boolean`
238238
* `http_reset_curlopt()` returns `boolean`

expected/http.out

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,60 +62,64 @@ from http(('GET', 'https://httpbin.org/anything', NULL, 'application/json', '{"s
6262

6363
-- DELETE
6464
SELECT status,
65-
content::json->'args' AS args,
65+
content::json->'args'->>'foo' AS args,
6666
content::json->'url' AS url,
6767
content::json->'method' AS method
6868
FROM http_delete('https://httpbin.org/anything?foo=bar');
69-
status | args | url | method
70-
--------+------------------+----------------------------------------+----------
71-
200 | { +| "https://httpbin.org/anything?foo=bar" | "DELETE"
72-
| "foo": "bar"+| |
73-
| } | |
69+
status | args | url | method
70+
--------+------+----------------------------------------+----------
71+
200 | bar | "https://httpbin.org/anything?foo=bar" | "DELETE"
72+
(1 row)
73+
74+
-- DELETE with payload
75+
SELECT status,
76+
content::json->'args'->>'foo' AS args,
77+
content::json->'url' AS url,
78+
content::json->'method' AS method,
79+
content::json->'data' AS data
80+
FROM http_delete('https://httpbin.org/anything?foo=bar', 'payload', 'text/plain');
81+
status | args | url | method | data
82+
--------+------+----------------------------------------+----------+-----------
83+
200 | bar | "https://httpbin.org/anything?foo=bar" | "DELETE" | "payload"
7484
(1 row)
7585

7686
-- PUT
7787
SELECT status,
7888
content::json->'data' AS data,
79-
content::json->'args' AS args,
89+
content::json->'args'->>'foo' AS args,
8090
content::json->'url' AS url,
8191
content::json->'method' AS method
8292
FROM http_put('https://httpbin.org/anything?foo=bar','payload','text/plain');
83-
status | data | args | url | method
84-
--------+-----------+------------------+----------------------------------------+--------
85-
200 | "payload" | { +| "https://httpbin.org/anything?foo=bar" | "PUT"
86-
| | "foo": "bar"+| |
87-
| | } | |
93+
status | data | args | url | method
94+
--------+-----------+------+----------------------------------------+--------
95+
200 | "payload" | bar | "https://httpbin.org/anything?foo=bar" | "PUT"
8896
(1 row)
8997

9098
-- PATCH
9199
SELECT status,
92100
content::json->'data' AS data,
93-
content::json->'args' AS args,
101+
content::json->'args'->>'foo' AS args,
94102
content::json->'url' AS url,
95103
content::json->'method' AS method
96104
FROM http_patch('https://httpbin.org/anything?foo=bar','{"this":"that"}','application/json');
97-
status | data | args | url | method
98-
--------+-----------------------+------------------+----------------------------------------+---------
99-
200 | "{\"this\":\"that\"}" | { +| "https://httpbin.org/anything?foo=bar" | "PATCH"
100-
| | "foo": "bar"+| |
101-
| | } | |
105+
status | data | args | url | method
106+
--------+-----------------------+------+----------------------------------------+---------
107+
200 | "{\"this\":\"that\"}" | bar | "https://httpbin.org/anything?foo=bar" | "PATCH"
102108
(1 row)
103109

104110
-- POST
105111
SELECT status,
106112
content::json->'data' AS data,
107-
content::json->'args' AS args,
113+
content::json->'args'->>'foo' AS args,
108114
content::json->'url' AS url,
109115
content::json->'method' AS method
110116
FROM http_post('https://httpbin.org/anything?foo=bar','payload','text/plain');
111-
status | data | args | url | method
112-
--------+-----------+------------------+----------------------------------------+--------
113-
200 | "payload" | { +| "https://httpbin.org/anything?foo=bar" | "POST"
114-
| | "foo": "bar"+| |
115-
| | } | |
117+
status | data | args | url | method
118+
--------+-----------+------+----------------------------------------+--------
119+
200 | "payload" | bar | "https://httpbin.org/anything?foo=bar" | "POST"
116120
(1 row)
117121

118-
-- POST with data
122+
-- POST with json data
119123
SELECT status,
120124
content::json->'form'->>'this' AS args,
121125
content::json->'url' AS url,
@@ -126,6 +130,18 @@ FROM http_post('https://httpbin.org/anything', jsonb_build_object('this', 'that'
126130
200 | that | "https://httpbin.org/anything" | "POST"
127131
(1 row)
128132

133+
-- POST with data
134+
SELECT status,
135+
content::json->'form'->>'key1' AS key1,
136+
content::json->'form'->>'key2' AS key2,
137+
content::json->'url' AS url,
138+
content::json->'method' AS method
139+
FROM http_post('https://httpbin.org/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded');
140+
status | key1 | key2 | url | method
141+
--------+--------+--------+--------------------------------+--------
142+
200 | value1 | value2 | "https://httpbin.org/anything" | "POST"
143+
(1 row)
144+
129145
-- HEAD
130146
SELECT lower(field) AS field, value
131147
FROM (

http--1.0--1.1.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,26 @@ CREATE OR REPLACE FUNCTION http(request http_request)
4343

4444
CREATE OR REPLACE FUNCTION http_get(uri VARCHAR)
4545
RETURNS http_response
46-
AS $$ SELECT http(('GET', $1, NULL, NULL, NULL)::http_request) $$
46+
AS $$ SELECT @extschema@.http(('GET', $1, NULL, NULL, NULL)::http_request) $$
4747
LANGUAGE 'sql';
4848

4949
CREATE OR REPLACE FUNCTION http_post(uri VARCHAR, content VARCHAR, content_type VARCHAR)
5050
RETURNS http_response
51-
AS $$ SELECT http(('POST', $1, NULL, $3, $2)::http_request) $$
51+
AS $$ SELECT @extschema@.http(('POST', $1, NULL, $3, $2)::http_request) $$
5252
LANGUAGE 'sql';
5353

5454
CREATE OR REPLACE FUNCTION http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR)
5555
RETURNS http_response
56-
AS $$ SELECT http(('PUT', $1, NULL, $3, $2)::http_request) $$
56+
AS $$ SELECT @extschema@.http(('PUT', $1, NULL, $3, $2)::http_request) $$
5757
LANGUAGE 'sql';
5858

5959
CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR)
6060
RETURNS http_response
61-
AS $$ SELECT http(('DELETE', $1, NULL, NULL, NULL)::http_request) $$
61+
AS $$ SELECT @extschema@.http(('DELETE', $1, NULL, NULL, NULL)::http_request) $$
6262
LANGUAGE 'sql';
6363

6464
CREATE OR REPLACE FUNCTION urlencode(string VARCHAR)
6565
RETURNS TEXT
6666
AS 'MODULE_PATHNAME'
6767
LANGUAGE 'c'
68-
IMMUTABLE STRICT;
68+
IMMUTABLE STRICT;

http--1.1--1.2.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ALTER DOMAIN http_method ADD CHECK (
1010

1111
CREATE OR REPLACE FUNCTION http_head(uri VARCHAR)
1212
RETURNS http_response
13-
AS $$ SELECT http(('HEAD', $1, NULL, NULL, NULL)::http_request) $$
13+
AS $$ SELECT @extschema@.http(('HEAD', $1, NULL, NULL, NULL)::http_request) $$
1414
LANGUAGE 'sql';
1515

1616
CREATE OR REPLACE FUNCTION http_set_curlopt (curlopt VARCHAR, value VARCHAR)

http--1.2--1.3.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ ALTER DOMAIN http_method ADD CHECK (
1010

1111
CREATE OR REPLACE FUNCTION http_patch(uri VARCHAR, content VARCHAR, content_type VARCHAR)
1212
RETURNS http_response
13-
AS $$ SELECT http(('PATCH', $1, NULL, $3, $2)::http_request) $$
13+
AS $$ SELECT @extschema@.http(('PATCH', $1, NULL, $3, $2)::http_request) $$
1414
LANGUAGE 'sql';

http--1.3--1.4.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ CREATE OR REPLACE FUNCTION urlencode(data JSONB)
1717

1818
CREATE OR REPLACE FUNCTION http_get(uri VARCHAR, data JSONB)
1919
RETURNS http_response
20-
AS $$ SELECT http(('GET', $1 || '?' || urlencode($2), NULL, NULL, NULL)::http_request) $$
20+
AS $$ SELECT @extschema@.http(('GET', $1 || '?' || urlencode($2), NULL, NULL, NULL)::http_request) $$
2121
LANGUAGE 'sql';
2222

2323
CREATE OR REPLACE FUNCTION http_post(uri VARCHAR, data JSONB)
2424
RETURNS http_response
25-
AS $$ SELECT http(('POST', $1, NULL, 'application/x-www-form-urlencoded', urlencode($2))::http_request) $$
25+
AS $$ SELECT @extschema@.http(('POST', $1, NULL, 'application/x-www-form-urlencoded', urlencode($2))::http_request) $$
2626
LANGUAGE 'sql';

http--1.4--1.5.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR, content VARCHAR, content_type VARCHAR)
3+
RETURNS http_response
4+
AS $$ SELECT @[email protected](('DELETE', $1, NULL, $3, $2)::@[email protected]_request) $$
5+
LANGUAGE 'sql';

http--1.4.sql renamed to http--1.5.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR)
8686
AS $$ SELECT @[email protected](('DELETE', $1, NULL, NULL, NULL)::@[email protected]_request) $$
8787
LANGUAGE 'sql';
8888

89+
CREATE OR REPLACE FUNCTION http_delete(uri VARCHAR, content VARCHAR, content_type VARCHAR)
90+
RETURNS http_response
91+
AS $$ SELECT @[email protected](('DELETE', $1, NULL, $3, $2)::@[email protected]_request) $$
92+
LANGUAGE 'sql';
93+
8994
CREATE OR REPLACE FUNCTION http_head(uri VARCHAR)
9095
RETURNS http_response
9196
AS $$ SELECT @[email protected](('HEAD', $1, NULL, NULL, NULL)::@[email protected]_request) $$

http.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
***********************************************************************/
2929

3030
/* Constants */
31-
#define HTTP_VERSION "1.4.0"
31+
#define HTTP_VERSION "1.5.0"
3232
#define HTTP_ENCODING "gzip"
3333
#define CURL_MIN_VERSION 0x071400 /* 7.20.0 */
3434

@@ -1138,7 +1138,7 @@ Datum http_request(PG_FUNCTION_ARGS)
11381138
headers = header_array_to_slist(array, headers);
11391139
}
11401140

1141-
/* If we have a payload we send it, assuming we're either POST, GET or PUT */
1141+
/* If we have a payload we send it, assuming we're either POST, GET, PATCH, PUT or DELETE */
11421142
if ( ! nulls[REQ_CONTENT] && values[REQ_CONTENT] )
11431143
{
11441144
text *content_text;
@@ -1160,15 +1160,21 @@ Datum http_request(PG_FUNCTION_ARGS)
11601160
content_text = DatumGetTextP(values[REQ_CONTENT]);
11611161
content_size = VARSIZE_ANY_EXHDR(content_text);
11621162

1163-
if ( method == HTTP_GET || method == HTTP_POST )
1163+
if ( method == HTTP_GET || method == HTTP_POST || method == HTTP_DELETE )
11641164
{
11651165
/* Add the content to the payload */
11661166
CURL_SETOPT(g_http_handle, CURLOPT_POST, 1);
11671167
if ( method == HTTP_GET )
11681168
{
11691169
/* Force the verb to be GET */
11701170
CURL_SETOPT(g_http_handle, CURLOPT_CUSTOMREQUEST, "GET");
1171+
}
1172+
else if( method == HTTP_DELETE )
1173+
{
1174+
/* Force the verb to be DELETE */
1175+
CURL_SETOPT(g_http_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
11711176
}
1177+
11721178
CURL_SETOPT(g_http_handle, CURLOPT_POSTFIELDS, text_to_cstring(content_text));
11731179
}
11741180
else if ( method == HTTP_PUT || method == HTTP_PATCH )

http.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
default_version = '1.4'
1+
default_version = '1.5'
22
module_pathname = '$libdir/http'
33
comment = 'HTTP client for PostgreSQL, allows web page retrieval inside the database.'

sql/http.sql

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,58 @@ from http(('GET', 'https://httpbin.org/anything', NULL, 'application/json', '{"s
3939

4040
-- DELETE
4141
SELECT status,
42-
content::json->'args' AS args,
42+
content::json->'args'->>'foo' AS args,
4343
content::json->'url' AS url,
4444
content::json->'method' AS method
4545
FROM http_delete('https://httpbin.org/anything?foo=bar');
4646

47+
-- DELETE with payload
48+
SELECT status,
49+
content::json->'args'->>'foo' AS args,
50+
content::json->'url' AS url,
51+
content::json->'method' AS method,
52+
content::json->'data' AS data
53+
FROM http_delete('https://httpbin.org/anything?foo=bar', 'payload', 'text/plain');
54+
4755
-- PUT
4856
SELECT status,
4957
content::json->'data' AS data,
50-
content::json->'args' AS args,
58+
content::json->'args'->>'foo' AS args,
5159
content::json->'url' AS url,
5260
content::json->'method' AS method
5361
FROM http_put('https://httpbin.org/anything?foo=bar','payload','text/plain');
5462

5563
-- PATCH
5664
SELECT status,
5765
content::json->'data' AS data,
58-
content::json->'args' AS args,
66+
content::json->'args'->>'foo' AS args,
5967
content::json->'url' AS url,
6068
content::json->'method' AS method
6169
FROM http_patch('https://httpbin.org/anything?foo=bar','{"this":"that"}','application/json');
6270

6371
-- POST
6472
SELECT status,
6573
content::json->'data' AS data,
66-
content::json->'args' AS args,
74+
content::json->'args'->>'foo' AS args,
6775
content::json->'url' AS url,
6876
content::json->'method' AS method
6977
FROM http_post('https://httpbin.org/anything?foo=bar','payload','text/plain');
7078

71-
-- POST with data
79+
-- POST with json data
7280
SELECT status,
7381
content::json->'form'->>'this' AS args,
7482
content::json->'url' AS url,
7583
content::json->'method' AS method
7684
FROM http_post('https://httpbin.org/anything', jsonb_build_object('this', 'that'));
7785

86+
-- POST with data
87+
SELECT status,
88+
content::json->'form'->>'key1' AS key1,
89+
content::json->'form'->>'key2' AS key2,
90+
content::json->'url' AS url,
91+
content::json->'method' AS method
92+
FROM http_post('https://httpbin.org/anything', 'key1=value1&key2=value2','application/x-www-form-urlencoded');
93+
7894
-- HEAD
7995
SELECT lower(field) AS field, value
8096
FROM (

0 commit comments

Comments
 (0)