Java 11 HttpClient
Posted: Fri Oct 05, 2018 10:38 am
Hi, I've been exploring the use of the JSON api with the new HttpClient in Java 11 and guided by this helpful documentation and the linked Python and Java code.
I haven't succeeded in logging in, and my best guess is that it's related to 'Referer'. When I try this:
it fails with the error
while skipping that line fails with a 403 Forbidden response.
This earlier topic suggests 'Referer' is essential. Comparable code in Python works for me if Referer is specified, but fails if it isn't; GET requests are also working fine from my Java/Groovy code.
Is there anything I'm clearly doing wrong, some other workaround, or should I be giving up on HttpClient for now?
Thanks,
Pete
I haven't succeeded in logging in, and my best guess is that it's related to 'Referer'. When I try this:
- Code: Select all
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.header("X-CSRFToken", this.token)
.header("Referer", url)
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
it fails with the error
- Code: Select all
restricted header name: "Referer"
while skipping that line fails with a 403 Forbidden response.
This earlier topic suggests 'Referer' is essential. Comparable code in Python works for me if Referer is specified, but fails if it isn't; GET requests are also working fine from my Java/Groovy code.
Is there anything I'm clearly doing wrong, some other workaround, or should I be giving up on HttpClient for now?
Thanks,
Pete