# \[tink\_http\] Client: how to gracefully deal with an invalid but successful response?

**URL:** https://community.haxe.org/t/tink-http-client-how-to-gracefully-deal-with-an-invalid-but-successful-response/2801
**Category:** Haxe
**Tags:** haxe-php, haxe-hl, web, haxe-jvm
**Created:** [December 12, 2020, 6:51pm UTC](https://community.haxe.org/t/tink-http-client-how-to-gracefully-deal-with-an-invalid-but-successful-response/2801 "2020-12-12T18:51:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cedx](https://community.haxe.org/user_avatar/community.haxe.org/cedx/32/2377_2.png) [@cedx](https://community.haxe.org/u/cedx)
#### Post date: [December 12, 2020, 6:51pm UTC](https://community.haxe.org/t/tink-http-client-how-to-gracefully-deal-with-an-invalid-but-successful-response/2801/1 "2020-12-12T18:51:51Z")

</div>

With `tink_http` v0.9.1, I use the `Client.fetch()` method to query a remote endpoint.  
This endpoint is accessible through a GET request and just returns a HTTP status code indicating the outcome: the response has no body, and its headers are not meaningful.

My issue: I get the following response when the request is successful.

```http
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 12 Dec 2020 13:58:02 GMT
Transfer-Encoding: chunked
Connection: close

```

If the compilation target is Node.js (the underlying client is `SecureNodeClient`), everything is fine.

But if the compilation target is a sys one (i.e. HashLink, JVM or PHP: the underlying client is `SecureSocketClient`), I’m starting to have errors.

~~With the JVM, the code does not compile! I encounter this issue: [Typing issue · Issue #14 · haxetink/tink\_chunk · GitHub](https://github.com/haxetink/tink_chunk/issues/14).~~  
(fixed: must use the latest Git version of `tink_chunk` instead of v0.3.1)

With other sys targets, the Tink HTTP client raises the following error:

```haxe
Error#500: Chunked encoding is not supported and the content-length header is required.

```

I’m using this kind of code to fix the error… but it’s very ugly:

```haxe
return
	try Client.fetch(url).map(outcome -> switch outcome {
		case Success(_):
			Success(Noise);

		case Failure(error):
			error.code == InternalError && ~/content-length header is required/i.match(error.message)
				? Success(Noise)
				: Failure(error);
	})
	catch (e) new Error(e.message);

```

So, is it possible to stop the HTTP client from parsing the response headers to avoid this error? I only need to check if the HTTP status is 200, and nothing else.

Also: with sys targets (i.e. it’s not required with Node.js), I must wrap the call with a `try/catch` because, contrary to my expectations, if a network error occurs an exception is raised instead of having a `Failure` outcome.

- HashLink: `Uncaught exception: SysError(Failed to connect)`
- PHP: `Fatal error: Uncaught ErrorException: socket_connect(): unable to connect [10061]`

---

<div class="post-metadata">

### Author: ![cedx](https://community.haxe.org/user_avatar/community.haxe.org/cedx/32/2377_2.png) [@cedx](https://community.haxe.org/u/cedx)
#### Post date: [December 14, 2020, 4:39pm UTC](https://community.haxe.org/t/tink-http-client-how-to-gracefully-deal-with-an-invalid-but-successful-response/2801/2 "2020-12-14T16:39:51Z")

</div>

Another issue: because of this behavior, my code fails to handle remote errors. When the remote service returns a `403` status (the response body is identical to a success one), the `Failure` is still the same: `500 Chunked encoding is not supported and the content-length header is required`. 😭

@kevinresol @back2dos Any idea?

---

<div class="post-metadata">

### Author: ![back2dos](https://community.haxe.org/user_avatar/community.haxe.org/back2dos/32/30_2.png) [@back2dos](https://community.haxe.org/u/back2dos)
#### Post date: [December 14, 2020, 5:01pm UTC](https://community.haxe.org/t/tink-http-client-how-to-gracefully-deal-with-an-invalid-but-successful-response/2801/3 "2020-12-14T17:01:24Z")

</div>

Please report on the appropriate issue tracker, ideally with a reproducible example 😉

---

<div class="post-metadata">

### Author: ![cedx](https://community.haxe.org/user_avatar/community.haxe.org/cedx/32/2377_2.png) [@cedx](https://community.haxe.org/u/cedx)
#### Post date: [December 14, 2020, 6:22pm UTC](https://community.haxe.org/t/tink-http-client-how-to-gracefully-deal-with-an-invalid-but-successful-response/2801/4 "2020-12-14T18:22:43Z")

</div>

Too bad: usually you always have the right answer for me 😄

> [@back2dos](#):
>
> Please report on the appropriate issue tracker

I will do… when I have a little more free time. I still have to look closely to the `tink_http` code and think about how to better formulate this problem (I’m old and slow 😔).
