Improve HTTPRequest default Content-Type

This commit is contained in:
Joshua Granick
2017-06-20 21:18:19 -07:00
parent 5f8c1712b1
commit 592e71fe47
2 changed files with 67 additions and 10 deletions

View File

@@ -96,8 +96,11 @@ class HTML5HTTPRequest {
}
request.open (Std.string (parent.method), uri, true);
if (parent.timeout > 0) {
request.timeout = parent.timeout;
}
if (binary) {
@@ -106,18 +109,45 @@ class HTML5HTTPRequest {
}
var hasContentType = false;
var contentType = null;
for (header in parent.headers) {
if (header.name == "Content-Type") hasContentType = true;
request.setRequestHeader (header.name, header.value);
if (header.name == "Content-Type") {
contentType = header.value;
} else {
request.setRequestHeader (header.name, header.value);
}
}
if (!hasContentType && parent.contentType != null) {
if (parent.contentType != null) {
request.setRequestHeader ("Content-Type", parent.contentType);
contentType = parent.contentType;
}
if (contentType == null) {
if (parent.data != null) {
contentType = "application/octet-stream";
} else if (query != "") {
contentType = "application/x-www-form-urlencoded";
}
}
if (contentType != null) {
request.setRequestHeader ("Content-Type", contentType);
}

View File

@@ -261,18 +261,45 @@ class NativeHTTPRequest {
var headers = [];
headers.push ("Expect: ");
var hasContentType = false;
var contentType = null;
for (header in cast (parent.headers, Array<Dynamic>)) {
if (header.name == "Content-Type") hasContentType = true;
headers.push ('${header.name}: ${header.value}');
if (header.name == "Content-Type") {
contentType = header.value;
} else {
headers.push ('${header.name}: ${header.value}');
}
}
if (!hasContentType) {
if (parent.contentType != null) {
headers.push ("Content-Type: " + parent.contentType);
contentType = parent.contentType;
}
if (contentType == null) {
if (parent.data != null) {
contentType = "application/octet-stream";
} else if (query != "") {
contentType = "application/x-www-form-urlencoded";
}
}
if (contentType != null) {
headers.push ("Content-Type: " + contentType);
}