62017

HTTP post with image and data

Question:

I am using apache HTTP Client for callin rest endpoints.

I want to call a POST request with image and some more form data as parameters. I can do them separately with to requests like first for the form data and the other for the image alone.

Is there any possible solution so that i can do it with one request.

Below is the api call

http://<url>?hint=hi&def=ready&image=<imagefile>

Answer1:

Use <a href="http://en.wikipedia.org/wiki/MIME#Multipart_messages" rel="nofollow">Multipart Request</a>. <a href="http://commons.apache.org/fileupload/" rel="nofollow">Commons Apache File Upload API</a> has very good API for the same. Apache Commons HttpClient has the API for client also.

Else, use Servlet 3.0 container which has support for multipart data. Where the <strong>String part</strong> type will be provided with <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29" rel="nofollow">HttpServletRequest.getParameter()</a> method. And the <strong>File type</strong> parts can be retrieved using <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html" rel="nofollow">HttpServletRequest.getPart()</a> method.

Recommend

  • how to supply a access token while making a graph api call
  • Laravel Route not working with Wamp
  • Measure heap used by each object in Java [closed]
  • Can't find cygwin setup.exe file?
  • Generating random numbers directly inside a .htaccess file
  • CUDA NSight is not installed with CUDA 5.0 installation file on Windows 8? [closed]
  • File extension of zlib zipped html page?
  • Uncaught TypeError: $(…).select2 is not a function
  • Cannot upload to OneDrive using the new SDK
  • Button click event not firing in jQuery
  • Grails calculated field in SQL
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • How to attach a node.js readable stream to a Sendgrid email?
  • JSON response opens as a file, but I can't access it with JavaScript
  • Why is an OPTIONS request sent to the server?
  • Spring security and special characters
  • Fetching methods from BroadcastReceiver to update UI
  • How to add date and time under each post in guestbook in google app engine
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • JSON with duplicate key names losing information when parsed
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Hazelcast - OperationTimeoutException
  • How to make Safari send if-modified-since header?
  • Web-crawler for facebook in python
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Return words with double consecutive letters
  • RestKit - RKRequestDelegate does not exist
  • Circular dependency while pushing http interceptor
  • Traverse Array and Display in markup
  • Linker errors when using intrinsic function via function pointer
  • Revoking OAuth Access Token Results in 404 Not Found
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal