Skip to content

Commit e478de1

Browse files
committed
fixed xhr calls pass the http headers
1 parent 3bdd3d1 commit e478de1

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.4.0+4
2+
3+
- Fixed issue on xhr requests - wasn't passing headers to the requests
4+
15
# 0.4.0+3
26

37
- Fixed dynamic library load for tests

assets/js/fetch.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function fetch(url, options) {
22
options = options || {};
3+
console.log('fetCH OPTIONS: '+ JSON.stringify(arguments));
34
return new Promise( (resolve, reject) => {
45
const request = new XMLHttpRequest();
56
const keys = [];
@@ -15,10 +16,10 @@ function fetch(url, options) {
1516
json: () => {
1617
// TODO: review this handle because it may discard \n from json attributes
1718
try {
18-
console.log('RESPONSE TEXT IN FETCH: ' + request.responseText);
19+
// console.log('RESPONSE TEXT IN FETCH: ' + request.responseText);
1920
return Promise.resolve(JSON.parse(request.responseText));
2021
} catch (e) {
21-
console.log('ERROR on fetch parsing JSON: ' + e.message);
22+
// console.log('ERROR on fetch parsing JSON: ' + e.message);
2223
return Promise.resolve(request.responseText);
2324
}
2425
},
@@ -47,8 +48,16 @@ function fetch(url, options) {
4748

4849
request.withCredentials = options.credentials=='include';
4950

50-
for (const i in options.headers) {
51-
request.setRequestHeader(i, options.headers[i]);
51+
if (options.headers) {
52+
if (options.headers.constructor.name == 'Object') {
53+
for (const i in options.headers) {
54+
request.setRequestHeader(i, options.headers[i]);
55+
}
56+
} else { // if it is some Headers pollyfill, the way to iterate is through for of
57+
for (const header of options.headers) {
58+
request.setRequestHeader(header[0], header[1]);
59+
}
60+
}
5261
}
5362

5463
request.send(options.body || null);

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ packages:
7575
path: ".."
7676
relative: true
7777
source: path
78-
version: "0.4.0+3"
78+
version: "0.4.0+4"
7979
flutter_test:
8080
dependency: "direct dev"
8181
description: flutter

lib/extensions/xhr.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,21 +366,21 @@ extension JavascriptRuntimeXhrExtension on JavascriptRuntime {
366366
try {
367367
String? method = arguments[0];
368368
String? url = arguments[1];
369-
List<String> headersList =
370-
(arguments[2] as List<dynamic>).cast<String>();
369+
dynamic headersList = arguments[2];
371370
String? body = arguments[3];
372371
int? idRequest = arguments[4];
373372

374373
Map<String, String> headers = {};
375-
headersList.forEach((value) {
376-
final headerMatch = regexpHeader.allMatches(value).first;
377-
String? headerName = headerMatch.group(0);
378-
String? headerValue = headerMatch.group(1);
379-
if (headerName != null) {
380-
headers[headerName] = headerValue ?? '';
381-
}
374+
headersList.forEach((header) {
375+
// final headerMatch = regexpHeader.allMatches(value).first;
376+
// String? headerName = headerMatch.group(0);
377+
// String? headerValue = headerMatch.group(1);
378+
// if (headerName != null) {
379+
// headers[headerName] = headerValue ?? '';
380+
// }
381+
String headerKey = header[0];
382+
headers[headerKey] = header[1];
382383
});
383-
384384
(dartContext[XHR_PENDING_CALLS_KEY] as List<dynamic>).add(
385385
XhrPendingCall(
386386
idRequest: idRequest,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: flutter_js
22
description: A Javascript engine to use with flutter.
33
It uses Quickjs on Android and JavascriptCore on IOS
4-
version: 0.4.0+3
4+
version: 0.4.0+4
55
homepage: https://github.com/abner/flutter_js
66
repository: https://github.com/abner/flutter_js
77

0 commit comments

Comments
 (0)