• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about setting Request Header for Rest web service

 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you issuing the request to the RESTful web service?
 
Jitesh Sinha
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic