# haxe.Http.requestUrl() throws InvalidAccessError on haxe-js

**URL:** <https://community.haxe.org/t/haxe-http-requesturl-throws-invalidaccesserror-on-haxe-js/4198>\
**Category:** Haxe\
**Tags:** haxe-js\
**Created:** [March 11, 2024, 9:34pm UTC](https://community.haxe.org/t/haxe-http-requesturl-throws-invalidaccesserror-on-haxe-js/4198 "2024-03-11T21:34:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zagortenej](https://community.haxe.org/user_avatar/community.haxe.org/zagortenej/32/848_2.png) [@zagortenej](https://community.haxe.org/u/zagortenej)\
**Post date:** [March 11, 2024, 9:34pm UTC](https://community.haxe.org/t/haxe-http-requesturl-throws-invalidaccesserror-on-haxe-js/4198/1 "2024-03-11T21:34:46Z")

</div>

Just tried `haxe.Http.requestUrl()` on `haxe-js` target and it throws `InvalidAccessError` in browser. Digged around a bit and found that this may happen if `XMLHttpRequest` is synchronous, which it is for `requestUrl`, and some conditions are met, for example `responseType` attribute is non-empty ([XMLHttpRequest Standard](https://xhr.spec.whatwg.org/#the-responsetype-attribute)) or `timeout` attribute is not zero ([XMLHttpRequest Standard](https://xhr.spec.whatwg.org/#the-open()-method)).

I’m on Haxe 4.3.3 and in [`std/haxe.http.HttpJs.hx` source file at line 138](https://github.com/HaxeFoundation/haxe/blob/52888e2600bccfef0f1fc2970c396b5096d2e2f6/std/haxe/http/HttpJs.hx#L138) the `responseType` is indeed set. If I comment out this line, the `InvalidAccessError` is not thrown, the `XMLHttpRequest` request returns content from the url, but `requestUrl` still fails for other reasons (namely, the implementation expects the response to be array buffer, and now it’s not).

So, off the bat, it looks like the `InvalidAccessError` is triggered by setting `responseType` and doing synchronous `XMLHttpRequest` request.

My code (that throws the error) is just this:

```haxe
function fetch_from_url( url : String ) : String {
  return Http.requestUrl( url );
}

```

where `Http` resolves to `haxe.http.HttpJs` for `haxe-js` target.

Is this a bug or am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![zagortenej](https://community.haxe.org/user_avatar/community.haxe.org/zagortenej/32/848_2.png) [@zagortenej](https://community.haxe.org/u/zagortenej)\
**Post date:** [March 11, 2024, 9:52pm UTC](https://community.haxe.org/t/haxe-http-requesturl-throws-invalidaccesserror-on-haxe-js/4198/2 "2024-03-11T21:52:18Z")

</div>

Apparently, `requestUrl()` is only **intended** to be used on `sys` target: [haxe.http.HttpBase - Haxe 4.3.4 API](https://api.haxe.org/haxe/http/HttpBase.html)  
RTFM I guess? 🤦‍♂️  
To my defence, that is mentioned in documentation for `HttpBase`, and not [HttpJs](https://api.haxe.org/haxe/http/HttpJs.html).  
On the other hand, maybe the doc for `HttpJs` `requestUrl()` should mention that it’s only intended for `sys` target?

---

<div class="post-metadata">

**Author:** ![zagortenej](https://community.haxe.org/user_avatar/community.haxe.org/zagortenej/32/848_2.png) [@zagortenej](https://community.haxe.org/u/zagortenej)\
**Post date:** [March 11, 2024, 10:05pm UTC](https://community.haxe.org/t/haxe-http-requesturl-throws-invalidaccesserror-on-haxe-js/4198/3 "2024-03-11T22:05:30Z")

</div>

For anyone interested, quick’n’dirty, happy-path-only sync GET request in browser:

```haxe
function fetch_from_url( url : String ) : String {
    var req = js.Browser.createXMLHttpRequest();
    // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open
    req.open("GET", url, false);
    // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send
    req.send();
    return req.responseText;
}

```

Keep in mind that this may not work except in a Worker, as noted here: [XMLHttpRequest: open() method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open#async)

Also, obviously, don’t do this in production 😸
