1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Add defaultHeaders flag that controls helpful default heders that can sometimes interfere with apis (#8152)

* Add defaultHeaders flag that controls helpful default heders that can sometimes interfere with apis

* Bump version number
This commit is contained in:
Matt Lauber 2024-05-29 06:42:50 -04:00 committed by GitHub
parent cd2d4b3eb7
commit 6910be795f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 5 deletions

View File

@ -39,6 +39,7 @@ exports.startup = function() {
method: params.method, method: params.method,
body: params.body, body: params.body,
binary: params.binary, binary: params.binary,
defaultHeaders: params.defaultHeaders,
oncompletion: params.oncompletion, oncompletion: params.oncompletion,
onprogress: params.onprogress, onprogress: params.onprogress,
bindStatus: params["bind-status"], bindStatus: params["bind-status"],

View File

@ -69,7 +69,7 @@ HttpClient.prototype.cancelAllHttpRequests = function() {
for(var t=this.requests.length - 1; t--; t>=0) { for(var t=this.requests.length - 1; t--; t>=0) {
var requestInfo = this.requests[t]; var requestInfo = this.requests[t];
requestInfo.request.cancel(); requestInfo.request.cancel();
} }
} }
this.requests = []; this.requests = [];
this.updateRequestTracker(); this.updateRequestTracker();
@ -112,6 +112,7 @@ function HttpClientRequest(options) {
this.method = options.method || "GET"; this.method = options.method || "GET";
this.body = options.body || ""; this.body = options.body || "";
this.binary = options.binary || ""; this.binary = options.binary || "";
this.defaultHeaders = options.defaultHeaders || true,
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) {
@ -231,7 +232,7 @@ Make an HTTP request. Options are:
exports.httpRequest = function(options) { exports.httpRequest = function(options) {
var type = options.type || "GET", var type = options.type || "GET",
url = options.url, url = options.url,
headers = options.headers || {accept: "application/json"}, headers = options.headers || (options.defaultHeaders ? {accept: "application/json"} : {}),
hasHeader = function(targetHeader) { hasHeader = function(targetHeader) {
targetHeader = targetHeader.toLowerCase(); targetHeader = targetHeader.toLowerCase();
var result = false; var result = false;
@ -257,7 +258,7 @@ exports.httpRequest = function(options) {
if(hasHeader("Content-Type") && ["application/x-www-form-urlencoded","multipart/form-data","text/plain"].indexOf(getHeader["Content-Type"]) === -1) { if(hasHeader("Content-Type") && ["application/x-www-form-urlencoded","multipart/form-data","text/plain"].indexOf(getHeader["Content-Type"]) === -1) {
return false; return false;
} }
return true; return true;
}, },
returnProp = options.returnProp || "responseText", returnProp = options.returnProp || "responseText",
request = new XMLHttpRequest(), request = new XMLHttpRequest(),
@ -307,10 +308,10 @@ exports.httpRequest = function(options) {
request.setRequestHeader(headerTitle,header); request.setRequestHeader(headerTitle,header);
}); });
} }
if(data && !hasHeader("Content-Type")) { if(data && !hasHeader("Content-Type") && options.defaultHeaders) {
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
} }
if(!hasHeader("X-Requested-With") && !isSimpleRequest(type,headers)) { if(!hasHeader("X-Requested-With") && !isSimpleRequest(type,headers) && options.defaultHeaders) {
request.setRequestHeader("X-Requested-With","TiddlyWiki"); request.setRequestHeader("X-Requested-With","TiddlyWiki");
} }
// Send data // Send data

View File

@ -19,6 +19,7 @@ The following parameters are used:
|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 | |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 |
|defaultHeaders |<<.from-version "5.3.4">> Defaults to true. Set to "false" to prevent default headers from being added. This can be helpful when dealing with apis that restrict header fields. |
|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 |