• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to return a link in a REST response  RSS feed

 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

I was reading some online posts and articles related to links (href) in a json/xml response.
When I looked up "hypermedia" I found some examples like this:



This would work just fine except for one detail. The protocol (http). What if my application is responsible for returning this link without knowing if the client uses http or https?
This way the client has to figure out what's the correct protocol and I'd like to avoid that but would this be acceptable?


Or this would be better?



Thanks in advance!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why wouldn't the REST API return the correct protocol? It knows it, after all.

Or, return protocol-less URIs.

You might also be interested in reading about HAL+JSON
 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
Hm, well, I think I need to give a little more detail...

My API returns something like this:


So, the first problem is, I'm serving files.
Second, they're under a static domain which is not the same as my app. The app is running under my-app.domain.com for example.
So this is not valid:


I have client apps running under http and https but right now if and https app uses my service it can't just render "http://static.mydomain.com/some-file.pdf" on a webpage because the browser will block the file, considering it an unsafe content.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why wouldn't the REST API return the correct protocol? It knows it, after all.
 
Ron McLeod
Saloon Keeper
Posts: 1432
207
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why wouldn't the REST API return the correct protocol? It knows it, after all.

If the web service server was behind a SSL/TLS termination proxy, it would see http even if the request used https.

Edit: I just saw that Amazon was saying they were going to add a X-Forwarded-Proto header to identify the original protocol to deal with this specific case.

Mateus Lucio wrote:My API returns something like this:


As Bear mentioned, you should be able to return a protocol-less/scheme-less reference such as:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The latter is my approach.
 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does work for me
I rushed myself and did not pay attention to the protocol-less uri recommendation.

This approach works perfectly!

Thank you Bear and Ron for your replies!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!