This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Question about setting Request Header for Rest web service Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Question about setting Request Header for Rest web service" Watch "Question about setting Request Header for Rest web service" New topic
Author

Question about setting Request Header for Rest web service

Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 145
I am writing a client code that needs to access a Rest web service.But I need to send authentication details in header so that it can be retrieved from Request header in the web service code.
I am using Spring and JSP to write the client code. Any idea how can I do it?
Any help will be appreciated.Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60749
    
  65

How are you issuing the request to the RESTful web service?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 145
I have put the web service URL as form action in JSP.Something like this -

<script language="javascript">
form.action = '/blah/service/request' ;
form.method='post' ;
form.submit() ;

</script>

<JSP code with form fields etc here>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60749
    
  65

You shouldn't have to set any headers. Just go ahead and make the request. If the RESTful web service needs basic authentication and is correctly written, it will return a 401 response which will trigger the browser to prompt you for credentials. Once you enter the credentials the browser will retry the request automatically with the appropriate header set.

If the web service ism't using HTTP correctly, or is not using HTTP authentication, you will not be able to submit requests to it using browser forms.
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 145
Thanks Bear.
Solution that you posted will not be acceptable because in my case it will not work the way you have mentioned.My client code needs to pass an authentication token as an attribute of the request.If I do not pass that token,I will get a server side error message.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60749
    
  65

So the service is not using HTTP authentication?

If not, you will not be able to use a form to make requests.

Have you considered Ajax? Or perhaps making the request from the server using Java?
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 145
Bear you are right.Service is not using HTTP authentication.How do I make request using Java and set token in request header?
My client is a Spring controller so I think I may be able to write some additional code in that to set request header.
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 145
Can someone please respond?
My main issue is setting request header .Can I even use filter to set request header considering that my code is a client code to a Rest Web service?If yes,how can I do that?
Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60749
    
  65

You'll need to use the low-level classes in the java.net package (e.g. URL, URLConnection), or a higher-level 3rd party library such as HttpClient.

If you are using Spring, I'd be surprised if that didn't already have something built into it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about setting Request Header for Rest web service
 
Similar Threads
Adding WS Security Info to Soap Header
Is there a way I can view the raw (HTTP) web service response?
Help needed for testing web service
REST security
Need some explanations about web-services basics.