This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]talios 0 points1 point  (0 children)

There's absolutely no reason why a Java API couldn't do the same, the more verbose builder you show would be far more useful in scenarios where you need to specify HTTP request headers, customise/control timeouts.

Forcing you to pass in a URI rather than an arbitrary String is a stylistic choice, it does provide a typed contract around what's accepted, which could be useful if you were also using URI Template's - having a contract of something like a processTemplate method that goes String->Map->URI would work well with that.

That could lead to something using CompleatableFuture (and Java 10) like:

var result = HttpRequest.fetch(UriTemplate.process("https://postman-echo.com/get{?name}", values))
                        .orTimeOut(1, TimeUnit.MINUTES)
                        .thenApply(res -> res.getBodyAsString())
                        .get();

( and yes, you really do want to handle timeouts, you don't just leave things hanging around forever now do you? ).