• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to upload file in REST webservice without using any GUI (html or jsp page)

 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have requirement to upload a file in rest web service. While doing google, i found several ways to upload it by providing some GUI but my senior don't
want any GUI.

Some other solutions I thought are FTP and /or copy past the file content into the XML and send to the REST server. But approach have security and other issues.
What other solution can be done?
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tushar,

You can create a POST method that accepts multipart/form-data. You can then send files with a simple AJAX request.
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan but i don't have any idea of AJAX. I am searching on google. Can you give me some example?

Also is it platform independent? We have to bundle it in our war file.
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also i see several examples on google. They all are using html page to upload it and then calling AJAX ..
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AJAX is not required, it's only one of the many ways to consume a RESTful API. If you want to make a client that doesn't run in a browser (for instance, a Java desktop application) you can also send requests to the REST method without AJAX. You can even upload files using HTML without JavaScript using a file input field.

What's important is that your method accepts POST requests with multipart/form-data. If you don't know how to do this, I suggest you look at the manual of your REST framework. If you need more than that, you should ask your senior what he really wants.
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already POST request using Jersey.. This is just a sample to show my idea..




I am having hard time for writing client end.
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i run the client through Postman file handling, it i throwing Null Pointer Exception at line 7. Possible reason i thought it doesn't found "file" word while requesting..
I also checked few on-line resources, they mentioned it is bug in Jersey.

Then i tried to make some html resource and that's working fine. But senior said they don't want gui stuff. They want some
client code which can call this POST. Here is html:


Then i found some Apache way to call this but having some issue there.. So i am still troubleshooting it.

Is there any way to call this POST through client code?
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You just wrote client code. You wrote it in HTML. If that's not what you want, you pick another language to write the client in. The client can be in JavaScript (using AJAX), or the client can be in Java (using HttpURLConnection, or something more convenient from a third party library).
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan.. I am able to do it finally. Here is my client//

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Have a cow
 
Tushar Goel
Ranch Hand
Posts: 931
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan for the cow..

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!