Fossil SCM

json-api: added examples of using wget/curl to the tips-and-tricks encoding section.

stephan 2020-01-29 05:46 trunk
Commit 946bb7e592b90faf82690a472e9da23b05ba0188ebce0b5675b12bf010d6d00a
1 file changed +42 -2
--- www/json-api/tips.md
+++ www/json-api/tips.md
@@ -2,10 +2,11 @@
22
([⬑JSON API Index](index.md))
33
44
Jump to:
55
66
* [Beware of Content-Type and Encoding...](#content-type)
7
+ * [Using `curl` and `wget`](#curl-wget)
78
* [Example JavaScript](#javascript)
89
* [Demo Apps](#demo-apps)
910
1011
---
1112
@@ -12,11 +13,11 @@
1213
<a id="content-type"></a>
1314
# Beware of Content-Type and Encoding...
1415
1516
When posting data to fossil, make sure that the request sends:
1617
17
-- **Content-Type** of application/json. Fossil also (currently)
18
+- **Content-Type** of `application/json`. Fossil also (currently)
1819
accepts `application/javascript` and `text/plain` as JSON input,
1920
but `application/json` is preferred. The client may optionally
2021
send `;charset=utf-8` with the Content-Type, but any other
2122
encoding produces undefined results. Behaviour without the charset
2223
or with `;charset=utf-8` suffix is identical.
@@ -36,11 +37,50 @@
3637
3738
The response will be (except in the case of an HTTP 500 error or
3839
similar) a JSON or JSONP string, ready to be parsed by your favourite
3940
`JSON.parse()` implementation or `eval()`'d directly.
4041
41
-<a href="javascript"></a>
42
+<a id="curl-wget"></a>
43
+## Using `curl` and `wget`
44
+
45
+Both [curl](https://curl.haxx.se/) and
46
+[wget](https://www.gnu.org/software/wget/) can be used to post data to
47
+this API from the command line or scripts, but both require an extra
48
+parameter to set the request encoding.
49
+
50
+Example:
51
+
52
+```console
53
+$ cat x.json
54
+{
55
+"payload": {
56
+ "sql": "SELECT * FROM reportfmt limit 1",
57
+ "format": "o"
58
+ }
59
+}
60
+
61
+# Fossil has been started locally with:
62
+# fossil server --localauth
63
+# which allows the following requests to work without extra
64
+# authenticaion:
65
+
66
+$ wget -q -O- \
67
+ --post-file=x.json \
68
+ --header="Content-Type: application/json" \
69
+ 'http://localhost:8080/json/query'
70
+
71
+$ curl \
72
+ --data-binary @x.json \
73
+ --header 'Content-Type: application/json' \
74
+ 'http://localhost:8080/json/query'
75
+```
76
+
77
+The relevant parts for encoding are the `--header` flag for `wget` and
78
+`curl`, noting that they have different syntaxes for each
79
+(`--header=X` vs `--header X`).
80
+
81
+<a id="javascript"></a>
4282
# Example JavaScript (Browser and Shell)
4383
4484
In the fossil source tree, [in the ajax directory](/dir/ajax), is test/demo code
4585
implemented in HTML+JavaScript. While it is still quite experimental, it
4686
demonstrates one approach to creating client-side wrapper APIs for
4787
--- www/json-api/tips.md
+++ www/json-api/tips.md
@@ -2,10 +2,11 @@
2 ([&#x2b11;JSON API Index](index.md))
3
4 Jump to:
5
6 * [Beware of Content-Type and Encoding...](#content-type)
 
7 * [Example JavaScript](#javascript)
8 * [Demo Apps](#demo-apps)
9
10 ---
11
@@ -12,11 +13,11 @@
12 <a id="content-type"></a>
13 # Beware of Content-Type and Encoding...
14
15 When posting data to fossil, make sure that the request sends:
16
17 - **Content-Type** of application/json. Fossil also (currently)
18 accepts `application/javascript` and `text/plain` as JSON input,
19 but `application/json` is preferred. The client may optionally
20 send `;charset=utf-8` with the Content-Type, but any other
21 encoding produces undefined results. Behaviour without the charset
22 or with `;charset=utf-8` suffix is identical.
@@ -36,11 +37,50 @@
36
37 The response will be (except in the case of an HTTP 500 error or
38 similar) a JSON or JSONP string, ready to be parsed by your favourite
39 `JSON.parse()` implementation or `eval()`'d directly.
40
41 <a href="javascript"></a>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42 # Example JavaScript (Browser and Shell)
43
44 In the fossil source tree, [in the ajax directory](/dir/ajax), is test/demo code
45 implemented in HTML+JavaScript. While it is still quite experimental, it
46 demonstrates one approach to creating client-side wrapper APIs for
47
--- www/json-api/tips.md
+++ www/json-api/tips.md
@@ -2,10 +2,11 @@
2 ([&#x2b11;JSON API Index](index.md))
3
4 Jump to:
5
6 * [Beware of Content-Type and Encoding...](#content-type)
7 * [Using `curl` and `wget`](#curl-wget)
8 * [Example JavaScript](#javascript)
9 * [Demo Apps](#demo-apps)
10
11 ---
12
@@ -12,11 +13,11 @@
13 <a id="content-type"></a>
14 # Beware of Content-Type and Encoding...
15
16 When posting data to fossil, make sure that the request sends:
17
18 - **Content-Type** of `application/json`. Fossil also (currently)
19 accepts `application/javascript` and `text/plain` as JSON input,
20 but `application/json` is preferred. The client may optionally
21 send `;charset=utf-8` with the Content-Type, but any other
22 encoding produces undefined results. Behaviour without the charset
23 or with `;charset=utf-8` suffix is identical.
@@ -36,11 +37,50 @@
37
38 The response will be (except in the case of an HTTP 500 error or
39 similar) a JSON or JSONP string, ready to be parsed by your favourite
40 `JSON.parse()` implementation or `eval()`'d directly.
41
42 <a id="curl-wget"></a>
43 ## Using `curl` and `wget`
44
45 Both [curl](https://curl.haxx.se/) and
46 [wget](https://www.gnu.org/software/wget/) can be used to post data to
47 this API from the command line or scripts, but both require an extra
48 parameter to set the request encoding.
49
50 Example:
51
52 ```console
53 $ cat x.json
54 {
55 "payload": {
56 "sql": "SELECT * FROM reportfmt limit 1",
57 "format": "o"
58 }
59 }
60
61 # Fossil has been started locally with:
62 # fossil server --localauth
63 # which allows the following requests to work without extra
64 # authenticaion:
65
66 $ wget -q -O- \
67 --post-file=x.json \
68 --header="Content-Type: application/json" \
69 'http://localhost:8080/json/query'
70
71 $ curl \
72 --data-binary @x.json \
73 --header 'Content-Type: application/json' \
74 'http://localhost:8080/json/query'
75 ```
76
77 The relevant parts for encoding are the `--header` flag for `wget` and
78 `curl`, noting that they have different syntaxes for each
79 (`--header=X` vs `--header X`).
80
81 <a id="javascript"></a>
82 # Example JavaScript (Browser and Shell)
83
84 In the fossil source tree, [in the ajax directory](/dir/ajax), is test/demo code
85 implemented in HTML+JavaScript. While it is still quite experimental, it
86 demonstrates one approach to creating client-side wrapper APIs for
87

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button