<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How to upload a File to a REST api</title><link>https://answers.axonivy.com/questions/3777/how-to-upload-a-file-to-a-rest-api</link><description>&lt;p&gt;I am building a document management service with a RESTful API. &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@Path("fileUpload")
public class FileUploadService
{
  @PUT
  @Produces(MediaType.APPLICATION_JSON)
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public Response uploadFile(@FormDataParam("file") InputStream fileStream,
          @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException
  {
   .... 
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It seems to work as expected if I use a third party client such as Postman. However, when using the REST activity of ivy I am unable to call this service. I always get errors stating the the header &lt;code&gt;X-Requested-By&lt;/code&gt; is missing. Whats the issue? How can I send files using a REST Client Call activity?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/3777/how-to-upload-a-file-to-a-rest-api" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 18 Apr 2019 04:19:34 -0400</lastBuildDate><item><title>Answer by ivy-jla</title><link>https://answers.axonivy.com/questions/3777/how-to-upload-a-file-to-a-rest-api/3779</link><description>&lt;p&gt;Hi. The problem with &lt;code&gt;X-Requested-By&lt;/code&gt; is that it shows up a lot even if it's not the problem. This issue was fixed in &lt;a href="https://jira.axonivy.com/jira/browse/XIVY-3097"&gt;XIVY-3097&lt;/a&gt;. So if you are sending a modyfing request like PUT or POST and you are sure that you set X-Requested-By Header then it's other issue. What your problem probably is, is that you need to set MediaType boundary like &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE;
contentType = Boundary.addBoundary(contentType);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and use it in your entity as content-type.
And it will not work if you use &lt;em&gt;MediaType.MULTIPART_FORM_DATA_TYPE&lt;/em&gt; instead of &lt;em&gt;contentType&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE;
contentType = Boundary.addBoundary(contentType);

Response apacheConnectorResponse = client
        .target(uri).request()
        .header("X-Requested-By", "ivy")
        .put(Entity.entity(createMultipart(realPdf), contentType));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You are able to see the full example in &lt;a href="https://developer.axonivy.com/permalink/lib/dev/connectivity-demos.iar"&gt;ConnectivityDemos (7.3 and newer)&lt;/a&gt; &amp;gt; rest &amp;gt; FileUpload&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">ivy-jla</dc:creator><pubDate>Thu, 18 Apr 2019 04:19:34 -0400</pubDate><guid>https://answers.axonivy.com/questions/3777/how-to-upload-a-file-to-a-rest-api/3779</guid></item></channel></rss>