# Functional Programming in Java, Second Edition: p.91: Rewrite FinanceData HTTP request method

**URL:** <https://forum.devtalk.com/t/functional-programming-in-java-second-edition-p-91-rewrite-financedata-http-request-method/105927>\
**Category:** PragProg Customers\
**Tags:** book-functional-programming-in-java-second-edition, errata\
**Created:** [21 April 2023 14:26 UTC](https://forum.devtalk.com/t/functional-programming-in-java-second-edition-p-91-rewrite-financedata-http-request-method/105927 "2023-04-21T14:26:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtonhofer](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dtonhofer/32/3389_2.png) [@dtonhofer](https://forum.devtalk.com/u/dtonhofer)\
**Post date:** [21 April 2023 14:26 UTC](https://forum.devtalk.com/t/functional-programming-in-java-second-edition-p-91-rewrite-financedata-http-request-method/105927/1 "2023-04-21T14:26:37Z")

</div>

On page 91, we use a short method to request a stock ticker, class `FinanceData`

However, this method is based on `java.net.URL` which has always been broken in several ways, one which being that it doesn’t encode the URL itself. That class is also deprecated in Java 20. The replacement is `java.net.URI` (since Java 7).

The construction of the query string, while appropriate as an exercise, can also be done better while demonstrating stream use.

Anyway, let’s go. Here is a definitely longer but IMHO nicer replacement:

```auto
    public class FinanceData {
        public static BigDecimal getPrice(final String ticker) {
            HttpResponse<String> response;
            try {
                final String scheme = "https";
                final String authority = "eodhistoricaldata.com";
                final String path = String.format("/api/eod/%s.US",ticker);
                final String query =
                        List.of("fmt=json", "filter=last_close", "api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX")
                                .stream()
                                .collect(Collectors.joining("&"));
                final String fragment = null;
                final URI uri = new URI(scheme,authority,path,query,fragment);
                System.out.println("Connecting to URI: " + uri);
                HttpClient client = HttpClient.newHttpClient();
                HttpRequest request = HttpRequest.newBuilder().uri(uri).build();
                response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
            } catch(Exception ex) {
                throw new RuntimeException(ex);
            }
            System.out.println("Received status code: " + response.statusCode());
            if (response.statusCode() == 200) {
                System.out.println("Received body: " + response.body());
                // parsing BigDecimal will throw on bad syntax
                return new BigDecimal(response.body());
            }
            else {
                throw new IllegalStateException("Status code was: " + response.statusCode());
            }
        }
    }

```

---

<div class="post-metadata">

**Author:** ![venkats](https://forum.devtalk.com/user_avatar/forum.devtalk.com/venkats/32/508_2.png) [@venkats](https://forum.devtalk.com/u/venkats)\
**Post date:** [31 May 2023 19:33 UTC](https://forum.devtalk.com/t/functional-programming-in-java-second-edition-p-91-rewrite-financedata-http-request-method/105927/2 "2023-05-31T19:33:55Z")

</div>

Decided to keep the change minimum here and resolve the deprecation warning. Fixed. Thank you.

---

<div class="post-metadata">

**Author:** ![dtonhofer](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dtonhofer/32/3389_2.png) [@dtonhofer](https://forum.devtalk.com/u/dtonhofer)\
**Post date:** [2 June 2023 08:36 UTC](https://forum.devtalk.com/t/functional-programming-in-java-second-edition-p-91-rewrite-financedata-http-request-method/105927/3 "2023-06-02T08:36:44Z")

</div>

Thank you.

That’s a damn shame (let’s just say I remember having to direct my companion devs forcefully away from using `URL` back in the early 00’s 😅 I think in the end we went with the [Apache HttpClient](https://hc.apache.org/httpcomponents-client-5.2.x/) …)

---

<div class="post-metadata">

**Author:** ![venkats](https://forum.devtalk.com/user_avatar/forum.devtalk.com/venkats/32/508_2.png) [@venkats](https://forum.devtalk.com/u/venkats)\
**Post date:** [2 June 2023 09:14 UTC](https://forum.devtalk.com/t/functional-programming-in-java-second-edition-p-91-rewrite-financedata-http-request-method/105927/4 "2023-06-02T09:14:13Z")

</div>

Practically, given the context of this example code, there is little benefit to the reader to see more code than minimally necessary for this function.
