We're Hiring!

Java 11 HttpClient

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

Java 11 HttpClient

Postby pbankhead » 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:
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
pbankhead
 
Posts: 11
Joined: Sun Oct 09, 2016 5:19 pm

Re: Java 11 HttpClient

Postby jburel » Mon Oct 08, 2018 8:45 pm

Hi Pete
you are ahead of us!
We have not been evaluating Java 11 yet.
We will need a bit of time to investigate the issue.

Cheers
Jmarie
User avatar
jburel
Team Member
 
Posts: 348
Joined: Thu May 21, 2009 6:38 pm
Location: dundee

Re: Java 11 HttpClient

Postby pbankhead » Wed Oct 24, 2018 11:03 am

Thanks Jmarie! It looks like it should be possible to set the Referer for the Java 11 HttpClient as of 11.0.2 (according to this). In the meantime if I switch back to using HttpsURLConnection it works fine.
pbankhead
 
Posts: 11
Joined: Sun Oct 09, 2016 5:19 pm


Return to Developer Discussion

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron