Introduction
In this post you will learn how to use SSIS REST API Task to perform REST API File Upload (i.e. RESTful file upload) to web server. Typically to access REST API you may have to use programming language but in this post we will use visual drag and drop approach.
File upload using HTTP/HTTPS protocol sometimes also known as multipart/form-data POST. Many REST APIs support for file upload using HTTP methods such as POST or PUT.
Real-world scenarios:
- Uploading Image/media to Twitter or Facebook
- Import Marketo Lead files using REST API
How to do REST API File Upload using SSIS
First you will have to Download FREE Trial of SSIS PowerPack (It comes with many REST API Connectors and other 30+ useful tasks/components)
- Download and install SSIS PowerPack from here
- From toolbox of SSIS designer drag ZS SSIS REST API Task
- Double click REST API Task to configure it
- Specify URL where you wish to POST file/other data
Example : http://myserver/api/v1/uploaddata - Select HTTP Request Method as POST ( make sure your API Documentation for this. Sometimes its PUT rather POST)
- Check File Upload option next to Body text box (This is most important so don’t forget 🙂 )
Method 1 – Upload file using Raw Mode (Or POST data from local file)
First method of uploading file or Posting data to server from local file is RAW upload. To enable this you simply have to make sure 2 things.
- Check File upload option next to Body
- In the Body enter local file path starting with @ sign (e.g. @c:\data\customers.csv)
When you don’t include Key/Value pair and only filepath found then request is treated as RAW Upload. In raw upload default content-type is set to application/octet-stream. If you want to set custom Content-Type then edit Headers in the Grid.
Above technique can be used to read POST data content from local file. Using this method only one file can be uploaded. If remote server supports multiple files (i.e. Multi-Part /Form Data POST) then use next method. See below screenshot of file content upload using Raw mode. We used Fiddler to inspect Web request made by REST API Task.
Method 2 – Upload file(s) using Multi-Part / Form Data mode (Mixed content, multiple files)
Second method for file upload is Multi-Part Upload. Advantage of using this method is you can POST upload key/value pair along with single or multiple files. To use this method you must make sure API supports it on the other end. Use below steps to POST data using Multi-Part method. SSIS PowerPack detect RAW mode vs Multi-Part POST based on BODY content. If Body contains Equal sign somewhere and File Upload option is checked then its treated as Multi-Part upload. If File Upload Option is checked and Body contains only file path which starts with @ sign then its treated as Raw content upload.
- Check File upload option next to Body
- Now click edit button next to Body text box. You may specify files / fields you wish to submit along with your request in the following format. Its key/value pair separated by new line. Each key/value pair has to be separated with equal sign (=).NOTE: Any value starting with at symbol (@) is treated as File Path. If you don’t want that behavior then use slash also known as escape sequence (e.g. Somefield3StartsWithAt=\@MyTwitterHandle). You can do same thing to add new line character in your value. use \r\n for new line. use \t for tabAlso notice name of field can be anything unless your API expects in certain way. Example: Twitter Image Upload API may expect your image file as media field in that case you would write …. media=@c:\data\someimage.png …. rather than File1=@c:\data\someimage.png
12345678File1=@c:\data\somedata.txtFile1.Content-Type=text/plainFile2=@c:\data\someimage.pngFile2.Content-Type=image/pngSomefield1=abcdefSomefield2=123456Extrafield3WithNewLine=This is line1\r\nThis is line2 with tab\t and more \t tabSomefield3StartsWithAt=\@MyTwitterHandleNOTE: If you do not specify Content-Type of file using SomeFieldName.Content-Type attribute then system automatically detects based on file extension. To exclude content-tye altogether you have to set it as blank string like below
12file1=@c:\temp\dump.jsonfile1.Content-Type= - Now click Test button on UI to make sure everything is looking good?
Understanding Multipart Upload / File Upload
Now lets take a look how does it look like in Fiddler when actual Multipart POST request is made for REST API File upload. Notice each field included in out HTTP POST request is separated by some random boundary. This boundary is automatically generated by REST API Task. If you post Image then you will see binary data rather than text for file content. Here is some known issue you need to be aware of.
Here is a Sample Multi-Part request.
Notice few things
- First line in the body content is Boundary.
- Last line in the body is also Boundary but it has two extra dash at the end after boundary line (i.e. ———–xyzzzzz– )
- Notice name=”something” . This is very important attribute. This is how target server knows which item to extract from your response.
- Content-Type is automatically determined if Multi-Part item is file upload. Based on extension ZappySys automatically determines most common content type. See next section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
POST https://somecompany.com/downloads/files/test/uploadtest.aspx HTTP/1.1 User-Agent: ZappySysApp/1.0.2019.10822 x-custom-hdr: 69c05bf5b6ef48dbb73c55020c50e027 Content-Type: multipart/form-data; boundary=---------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825 Host: somecompany.com Content-Length: 3619 Expect: 100-continue Accept-Encoding: gzip, deflate Connection: Keep-Alive -----------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825 Content-Disposition: form-data; name="File1"; filename="MultiPartFileUpload1.txt" Content-Type: text/plain A000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -----------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825 Content-Disposition: form-data; name="File2"; filename="MultiPartFileUpload2.txt" Content-Type: text/plain B00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -----------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825 Content-Disposition: form-data; name="Extrafield1" aaaa -----------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825 Content-Disposition: form-data; name="Extrafield2" bbbb -----------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825 Content-Disposition: form-data; name="Extrafield3WithNewLine" This is line1 This is line2 with tab tab tab -----------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825 Content-Disposition: form-data; name="Extrafield3WithAt" @MyTwitterHandle -----------------------------WebKitFormBoundary55fd218185294c408e9ef21a4ac82825-- |
Known Issues
Some time you may receive below error if your server is expecting \r\n after last Multi-Part boundary. Here is the fix for this error.
1 2 3 4 5 6 7 8 |
The remote server returned an error: (500) Internal Server Error. Response Url: https://your-host/api Status Code: InternalServerError Response Body: {"Message":"An error has occurred." ,"ExceptionMessage":"Unexpected end of MIME multipart stream. MIME multipart message is not complete.","ExceptionType":"System.IO.IOException","StackTrace":" at System.Net.Http.Formatting.Parsers.MimeMultipartBodyPartParser.<ParseBuffer>d__0.MoveNext()\r\n at System.Net.Http.HttpContentMultipartExtensions.MoveNextPart(MultipartAsyncContext context)" } |
Most common extension and Content-Type
In the multi-part upload you might have noticed that ZappySys adds content-type for some file extensions. Here is full list of some common extensions and its content-type
Conclusion
If you have need to perform REST API File Upload / REST API integration without learning complex APIs and programming languages (e.g. Python, JAVA, C#) then SSIS PowerPack provides perfect way to do REST API integration with your existing system. You can uncover endless possibilities with drag and drop approach which is not only simple to use but includes many options for complex scenarios too.
Keywords
How to perform rest api file upload | How to do RESTful file upload in C# | How to do multipart file upload without cURL | How to upload multiple files using http multi part forms | Rest Client Upload File | C# REST API Upload File | Rest API Upload Image | Rest API Upload File