From 75796ea14cb22650b137ec6f3f98867125b23334 Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Sat, 17 Oct 2015 19:04:00 +0300 Subject: [PATCH] Ignore request body for XHR GET requests. Raises an error on Android. Caused by the Angular XHR backend sending an empty string. --- apps/tests/xhr-tests.ts | 8 ++++++++ xhr/xhr.ts | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/tests/xhr-tests.ts b/apps/tests/xhr-tests.ts index 9756fa880..cab72997f 100644 --- a/apps/tests/xhr-tests.ts +++ b/apps/tests/xhr-tests.ts @@ -212,6 +212,14 @@ export var test_XMLHttpRequest_requestShouldBePossibleAfterAbort = function (don xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); }; +export function test_ignore_zero_length_request_body(done) { + let xhr = new XMLHttpRequest(); + xhr.open("GET", "https://httpbin.org/get"); + + xhr.send(''); + done(null); +} + export function test_raises_onload_Event(done) { let xhr = new XMLHttpRequest(); xhr.onload = () => { diff --git a/xhr/xhr.ts b/xhr/xhr.ts index f4268cfed..bd07470c1 100644 --- a/xhr/xhr.ts +++ b/xhr/xhr.ts @@ -72,7 +72,10 @@ export class XMLHttpRequest { this._status = null; if (types.isDefined(this._options)) { - if (types.isString(data)) { + if (types.isString(data) && this._options.method !== 'GET') { + //The Android Java HTTP lib throws an exception if we provide a + //a request body for GET requests, so we avoid doing that. + //Browser implementations silently ignore it as well. this._options.content = data; } else if (data instanceof FormData) { this._options.content = (data).toString();