mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-26 19:47:20 +00:00
tm-http-request: Add support for binary responses
With a demo courtesy of https://random.dog/ @rmunn you recently worked on the base64 utilities. I tried to use $tw.utils.base64Encode instead of window.btoa, but found that it didn't work. It's concerning because we expose that utility method as a filter operation, and it would be frustrating if we were not base64encoding things properly.
This commit is contained in:
parent
7182dbf244
commit
9b2af13596
@ -38,6 +38,7 @@ exports.startup = function() {
|
|||||||
url: params.url,
|
url: params.url,
|
||||||
method: params.method,
|
method: params.method,
|
||||||
body: params.body,
|
body: params.body,
|
||||||
|
binary: params.binary,
|
||||||
oncompletion: params.oncompletion,
|
oncompletion: params.oncompletion,
|
||||||
onprogress: params.onprogress,
|
onprogress: params.onprogress,
|
||||||
bindStatus: params["bind-status"],
|
bindStatus: params["bind-status"],
|
||||||
|
@ -90,6 +90,7 @@ wiki: wiki to be used for executing action strings
|
|||||||
url: URL for request
|
url: URL for request
|
||||||
method: method eg GET, POST
|
method: method eg GET, POST
|
||||||
body: text of request body
|
body: text of request body
|
||||||
|
binary: set to "yes" to force binary processing of response payload
|
||||||
oncompletion: action string to be invoked on completion
|
oncompletion: action string to be invoked on completion
|
||||||
onprogress: action string to be invoked on progress updates
|
onprogress: action string to be invoked on progress updates
|
||||||
bindStatus: optional title of tiddler to which status ("pending", "complete", "error") should be written
|
bindStatus: optional title of tiddler to which status ("pending", "complete", "error") should be written
|
||||||
@ -110,6 +111,7 @@ function HttpClientRequest(options) {
|
|||||||
this.bindProgress = options["bindProgress"];
|
this.bindProgress = options["bindProgress"];
|
||||||
this.method = options.method || "GET";
|
this.method = options.method || "GET";
|
||||||
this.body = options.body || "";
|
this.body = options.body || "";
|
||||||
|
this.binary = options.binary || "";
|
||||||
this.variables = options.variables;
|
this.variables = options.variables;
|
||||||
var url = options.url;
|
var url = options.url;
|
||||||
$tw.utils.each(options.queryStrings,function(value,name) {
|
$tw.utils.each(options.queryStrings,function(value,name) {
|
||||||
@ -156,6 +158,8 @@ HttpClientRequest.prototype.send = function(callback) {
|
|||||||
type: this.method,
|
type: this.method,
|
||||||
headers: this.requestHeaders,
|
headers: this.requestHeaders,
|
||||||
data: this.body,
|
data: this.body,
|
||||||
|
returnProp: this.binary === "" ? "responseText" : "response",
|
||||||
|
responseType: this.binary === "" ? "text" : "arraybuffer",
|
||||||
callback: function(err,data,xhr) {
|
callback: function(err,data,xhr) {
|
||||||
var hasSucceeded = xhr.status >= 200 && xhr.status < 300,
|
var hasSucceeded = xhr.status >= 200 && xhr.status < 300,
|
||||||
completionCode = hasSucceeded ? "complete" : "error",
|
completionCode = hasSucceeded ? "complete" : "error",
|
||||||
@ -175,6 +179,16 @@ HttpClientRequest.prototype.send = function(callback) {
|
|||||||
data: (data || "").toString(),
|
data: (data || "").toString(),
|
||||||
headers: JSON.stringify(headers)
|
headers: JSON.stringify(headers)
|
||||||
};
|
};
|
||||||
|
/* Convert data from binary to base64 */
|
||||||
|
if (xhr.responseType === "arraybuffer") {
|
||||||
|
var binary = "",
|
||||||
|
bytes = new Uint8Array(data),
|
||||||
|
len = bytes.byteLength;
|
||||||
|
for (var i=0; i<len; i++) {
|
||||||
|
binary += String.fromCharCode(bytes[i]);
|
||||||
|
}
|
||||||
|
resultVariables.data = window.btoa(binary);
|
||||||
|
}
|
||||||
self.wiki.addTiddler(new $tw.Tiddler(self.wiki.getTiddler(requestTrackerTitle),{
|
self.wiki.addTiddler(new $tw.Tiddler(self.wiki.getTiddler(requestTrackerTitle),{
|
||||||
status: completionCode,
|
status: completionCode,
|
||||||
}));
|
}));
|
||||||
@ -212,6 +226,7 @@ Make an HTTP request. Options are:
|
|||||||
callback: function invoked with (err,data,xhr)
|
callback: function invoked with (err,data,xhr)
|
||||||
progress: optional function invoked with (lengthComputable,loaded,total)
|
progress: optional function invoked with (lengthComputable,loaded,total)
|
||||||
returnProp: string name of the property to return as first argument of callback
|
returnProp: string name of the property to return as first argument of callback
|
||||||
|
responseType: "text" or "arraybuffer"
|
||||||
*/
|
*/
|
||||||
exports.httpRequest = function(options) {
|
exports.httpRequest = function(options) {
|
||||||
var type = options.type || "GET",
|
var type = options.type || "GET",
|
||||||
@ -264,6 +279,7 @@ exports.httpRequest = function(options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
request.responseType = options.responseType || "text";
|
||||||
// Set up the state change handler
|
// Set up the state change handler
|
||||||
request.onreadystatechange = function() {
|
request.onreadystatechange = function() {
|
||||||
if(this.readyState === 4) {
|
if(this.readyState === 4) {
|
||||||
|
@ -0,0 +1,88 @@
|
|||||||
|
title: WidgetMessage: tm-http-request Example - Random Dog
|
||||||
|
tags: $:/tags/Global
|
||||||
|
|
||||||
|
\procedure download-dog(url)
|
||||||
|
|
||||||
|
\procedure completion-download-dog()
|
||||||
|
\import [subfilter{$:/core/config/GlobalImportFilter}]
|
||||||
|
<$action-log msg="In completion-download-dog"/>
|
||||||
|
<$action-log/>
|
||||||
|
<!-- Success -->
|
||||||
|
<$list filter="[<status>compare:number:gteq[200]compare:number:lteq[299]]" variable="ignore">
|
||||||
|
<!-- Create the dog tiddler -->
|
||||||
|
<$action-createtiddler
|
||||||
|
$basetitle=`$:/RandomDog/$(title)$`
|
||||||
|
text=<<data>>
|
||||||
|
tags="$:/tags/RandomDog"
|
||||||
|
type={{{ [<headers>jsonget[content-type]] }}}
|
||||||
|
credits="https://random.dog/"
|
||||||
|
>
|
||||||
|
<$action-log msg="Created tiddler" title=<<createTiddler-title>>/>
|
||||||
|
</$createtiddler>
|
||||||
|
</$list>
|
||||||
|
\end completion-download-dog
|
||||||
|
|
||||||
|
<$action-sendmessage
|
||||||
|
$message="tm-http-request"
|
||||||
|
url=<<url>>
|
||||||
|
method="GET"
|
||||||
|
binary="yes"
|
||||||
|
oncompletion=<<completion-download-dog>>
|
||||||
|
var-title=<<url>>
|
||||||
|
/>
|
||||||
|
\end download-dog
|
||||||
|
|
||||||
|
\procedure get-random-dog()
|
||||||
|
|
||||||
|
\procedure completion-get-json()
|
||||||
|
\import [subfilter{$:/core/config/GlobalImportFilter}]
|
||||||
|
<$action-log msg="In completion-get-json"/>
|
||||||
|
<$action-log/>
|
||||||
|
<!-- Success -->
|
||||||
|
<$list filter="[<status>compare:number:gteq[200]compare:number:lteq[299]]" variable="ignore">
|
||||||
|
<!-- Download the dog -->
|
||||||
|
<$macrocall $name="download-dog" url={{{ [<data>jsonget[url]] }}}/>
|
||||||
|
</$list>
|
||||||
|
\end completion-get-json
|
||||||
|
|
||||||
|
<$action-sendmessage
|
||||||
|
$message="tm-http-request"
|
||||||
|
url="https://random.dog/woof.json"
|
||||||
|
method="GET"
|
||||||
|
oncompletion=<<completion-get-json>>
|
||||||
|
/>
|
||||||
|
\end get-random-dog
|
||||||
|
|
||||||
|
!! Random Dogs
|
||||||
|
|
||||||
|
This demo uses the API of the website https://random.dog/ to import a random dog image or video.
|
||||||
|
|
||||||
|
<$button actions=<<get-random-dog>>>
|
||||||
|
Import a random dog image or video
|
||||||
|
</$button>
|
||||||
|
|
||||||
|
<$list filter="[tag[$:/tags/RandomDog]limit[1]]" variable="ignore">
|
||||||
|
|
||||||
|
!! Imported Tiddlers
|
||||||
|
|
||||||
|
<$button>
|
||||||
|
<$action-deletetiddler $filter="[tag[$:/tags/RandomDog]]"/>
|
||||||
|
Delete all imported random dogs
|
||||||
|
</$button>
|
||||||
|
|
||||||
|
Export all imported random dogs: <$macrocall $name="exportButton" exportFilter="[tag[$:/tags/RandomDog]]" lingoBase="$:/language/Buttons/ExportTiddlers/"/>
|
||||||
|
|
||||||
|
</$list>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<$list filter="[tag[$:/tags/RandomDog]!sort[modified]]">
|
||||||
|
<li>
|
||||||
|
<$link>
|
||||||
|
<$text text=<<currentTiddler>>/>
|
||||||
|
</$link>
|
||||||
|
<div style="width:300px;height:300px;">
|
||||||
|
<$transclude $tiddler=<<currentTiddler>>/>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
</$list>
|
||||||
|
</ol>
|
@ -1,5 +1,5 @@
|
|||||||
title: WidgetMessage: tm-http-request Example - Zotero
|
title: WidgetMessage: tm-http-request Example - Zotero
|
||||||
tags: $:/tags/Macro
|
tags: $:/tags/Global
|
||||||
|
|
||||||
\procedure select-zotero-group()
|
\procedure select-zotero-group()
|
||||||
Specify the Zotero group ID to import
|
Specify the Zotero group ID to import
|
||||||
@ -34,7 +34,7 @@ Specify the Zotero group ID to import
|
|||||||
\procedure zotero-get-items(start:"0",limit:"25")
|
\procedure zotero-get-items(start:"0",limit:"25")
|
||||||
|
|
||||||
\procedure completion()
|
\procedure completion()
|
||||||
\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]]
|
\import [subfilter{$:/core/config/GlobalImportFilter}]
|
||||||
<$action-log msg="In completion"/>
|
<$action-log msg="In completion"/>
|
||||||
<$action-log/>
|
<$action-log/>
|
||||||
<!-- Success -->
|
<!-- Success -->
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
caption: tm-http-request
|
caption: tm-http-request
|
||||||
created: 20230429161453032
|
created: 20230429161453032
|
||||||
modified: 20230429161453032
|
modified: 20230717104212742
|
||||||
tags: Messages
|
tags: Messages
|
||||||
title: WidgetMessage: tm-http-request
|
title: WidgetMessage: tm-http-request
|
||||||
type: text/vnd.tiddlywiki
|
type: text/vnd.tiddlywiki
|
||||||
@ -18,6 +18,7 @@ The following parameters are used:
|
|||||||
|!Name |!Description |
|
|!Name |!Description |
|
||||||
|method |HTTP method (eg "GET", "POST") |
|
|method |HTTP method (eg "GET", "POST") |
|
||||||
|body |String data to be sent with the request |
|
|body |String data to be sent with the request |
|
||||||
|
|binary |<<.from-version "5.3.1">> Set to "yes" to cause the response body to be treated as binary data and returned in base64 format |
|
||||||
|query-* |Query string parameters with string values |
|
|query-* |Query string parameters with string values |
|
||||||
|header-* |Headers with string values |
|
|header-* |Headers with string values |
|
||||||
|password-header-* |Headers with values taken from the password store |
|
|password-header-* |Headers with values taken from the password store |
|
||||||
@ -49,3 +50,4 @@ Note that the state tiddler $:/state/http-requests contains a number representin
|
|||||||
!! Examples
|
!! Examples
|
||||||
|
|
||||||
* [[Zotero's|https://www.zotero.org/]] API for retrieving reference items: [[WidgetMessage: tm-http-request Example - Zotero]]
|
* [[Zotero's|https://www.zotero.org/]] API for retrieving reference items: [[WidgetMessage: tm-http-request Example - Zotero]]
|
||||||
|
* [[Random Dog's|https://random.dog/]] API for retrieving random pictures of dogs showing how to retrieve binary data: [[WidgetMessage: tm-http-request Example - Random Dogs]]
|
||||||
|
Loading…
Reference in New Issue
Block a user