Fossil SCM
json-api: added examples of using wget/curl to the tips-and-tricks encoding section.
Commit
946bb7e592b90faf82690a472e9da23b05ba0188ebce0b5675b12bf010d6d00a
Parent
6b1786659543cee…
1 file changed
+42
-2
+42
-2
| --- www/json-api/tips.md | ||
| +++ www/json-api/tips.md | ||
| @@ -2,10 +2,11 @@ | ||
| 2 | 2 | ([⬑JSON API Index](index.md)) |
| 3 | 3 | |
| 4 | 4 | Jump to: |
| 5 | 5 | |
| 6 | 6 | * [Beware of Content-Type and Encoding...](#content-type) |
| 7 | + * [Using `curl` and `wget`](#curl-wget) | |
| 7 | 8 | * [Example JavaScript](#javascript) |
| 8 | 9 | * [Demo Apps](#demo-apps) |
| 9 | 10 | |
| 10 | 11 | --- |
| 11 | 12 | |
| @@ -12,11 +13,11 @@ | ||
| 12 | 13 | <a id="content-type"></a> |
| 13 | 14 | # Beware of Content-Type and Encoding... |
| 14 | 15 | |
| 15 | 16 | When posting data to fossil, make sure that the request sends: |
| 16 | 17 | |
| 17 | -- **Content-Type** of application/json. Fossil also (currently) | |
| 18 | +- **Content-Type** of `application/json`. Fossil also (currently) | |
| 18 | 19 | accepts `application/javascript` and `text/plain` as JSON input, |
| 19 | 20 | but `application/json` is preferred. The client may optionally |
| 20 | 21 | send `;charset=utf-8` with the Content-Type, but any other |
| 21 | 22 | encoding produces undefined results. Behaviour without the charset |
| 22 | 23 | or with `;charset=utf-8` suffix is identical. |
| @@ -36,11 +37,50 @@ | ||
| 36 | 37 | |
| 37 | 38 | The response will be (except in the case of an HTTP 500 error or |
| 38 | 39 | similar) a JSON or JSONP string, ready to be parsed by your favourite |
| 39 | 40 | `JSON.parse()` implementation or `eval()`'d directly. |
| 40 | 41 | |
| 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> | |
| 42 | 82 | # Example JavaScript (Browser and Shell) |
| 43 | 83 | |
| 44 | 84 | In the fossil source tree, [in the ajax directory](/dir/ajax), is test/demo code |
| 45 | 85 | implemented in HTML+JavaScript. While it is still quite experimental, it |
| 46 | 86 | demonstrates one approach to creating client-side wrapper APIs for |
| 47 | 87 |
| --- www/json-api/tips.md | |
| +++ www/json-api/tips.md | |
| @@ -2,10 +2,11 @@ | |
| 2 | ([⬑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 | ([⬑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 |