• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to set the content type for a GET request from a browser

 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Is there a way by which we can set the content type while accessing from a browser?

like when i am accessing, http://localhost:8080/something , i want to set the content type also.

I am able to do it from the java code but still need to see if it can be done from the browser.

Thank you
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure I understand your question, the content type of the response is defined by the server? What is it you are trying to achieve?

Some browsers (such as IE) will try to perform content type sniffing if none is defined in the response. It's very difficult to tailor this behaviour, you would need to update a whole bunch of stuff in the registry to do it.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the browser depends on the context to set the request content type options in the Accept headers.

Try the Live HTTP Headers tool with firefox to see exactly what your browser is sending in the request.

Bill
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Sturrock wrote:Not sure I understand your question, the content type of the response is defined by the server? What is it you are trying to achieve?



Hi Paul,

The requirement is that the clients can ask for the JSON or the XML version of a record or a set of records using a GET request. So i was thinking to set the content-type requested by the GET request so that the servlet can convert the data object into either JSON or XML.
Normally, when from a browser, we do a http://localhost:8080/appName/servletName, its a GET request but no content-type is set.

However the same can be set through a Java client to make a GET request like


and the same can be obtained in the servlet as


So, i was wondering if there was a way to set this property from the browser(but not a request parameters)

Thank you
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the server to produce one of two formats based on something in the request, the obvious thing to do is to pass a parameter defining the format. This has the advantage that you don't have to do weird things like hacking the browser to make it work, it is just a standard feature of HTML.

 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: Content-type in a request refers to the type of the data you are sending!

It is the Accept header that gives the type you want.

Here is the Wikipedia convenient page for HTTP headers.

Bill
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh...you are right Bill,

i was confused between the two.

I think i will go ahead with the way suggested by Paul to pass the parameter in the request instead.

Thank you

Rahul
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or consider using the extension of the request as a response type indicator, like "foo.json" returns JSON, "foo.xml" returns XML, "foo.jsp" returns HTML.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

I am using Jersey framework for this and eventually i want the requests like foo.json or foo.xml to go to a single method in the Servlet, but i am not sure whether if my urls are different in these cases(foo.json or foo.xml), how will jersey handle the request and how will i read the ".json" part.

Also, i think Jersey provides an annotated way of handling both xml and json in the same method using jaxb but i am not using it because of some other related complexities. so i am using a custom way to convert object to xml or json

Thank you
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i want the requests like foo.json or foo.xml to go to a single method



Why in the name of sanity would you want that? It seems to me that the whole idea of jersey is to direct the request to the method designed to return a specific type as set by the annotation.

You are just headed for mass confusion.

See this JAX-RS overview.

Bill
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a large no of methods written for returning the XML type data, and repeating the code for all the methods just because of a different type of data conversion doesn't look good to me.

Because after some other time, a different content type requirement might come, and i dont want to rewrite all the methods just because the content type is different(unless the business logic requires some different processing for different content type)

So, what i am having right now, is that i get my data object from the backend to the jersey servlet and then based on the request parameter convert it to either JSON or XML(and that code is common).

Also, it looks like with Jersey, we can specify 2 or more content types for the same method, and that seems to be ok for what i am trying to do.

Thank you
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic