• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Best way to return a link in a REST response

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Why wouldn't the REST API return the correct protocol? It knows it, after all.

 
Marshal
Posts: 4600
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The latter is my approach.
 
Mateus Lucio
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Those are the largest trousers in the world! Especially when next to this ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic