Code covered by the BSD License  

Highlights from
urlreadpost - url POST method with binary file uploading

5.0

5.0 | 1 rating Rate this file 16 Downloads (last 30 days) File Size: 3.5 KB File ID: #27189

urlreadpost - url POST method with binary file uploading

by Dan Ellis

 

07 Apr 2010 (Updated 08 Apr 2010)

A replacement for urlread(url,'post',...) that allows POST to upload binary (file) data.

| Watch this File

File Information
Description

The HTTP 'POST' method is provided as a mechanism for uploading data as part of a URL request. There are two methods for encoding this data: application/x-www-form-urlencoded, in which each parameter is inserted as part of one long string, and multipart/form-data, in which each parameter gets its own MIME form-data block in the stream. This latter method allows the inclusion of large amounts of binary data (for instance, a POST which uploads a file) which is not possible with x-www-form-urlencoded.

Matlab's stock urlread does support the 'POST' method, but only with the x-www-form-urlencoded encoding. I wrote this replacement to be able to access web services that involve uploading binary files directly from Matlab. Here's an example, of uploading MP3 audio data to the Echo Nest Analyze API:

  f = fopen('music.mp3');
  d = fread(f,Inf,'*uint8'); % Read in byte stream of MP3 file
  fclose(f);
  str = urlreadpost('http://developer.echonest.com/api/upload', ...
          {'file',d,'version','3','api_key','API-KEY','wait','Y'});

urlpost checks the type of the value part of each key/value pair, and encodes it as an octet-stream (binary) if it is not
char data. In practice, you should only pass byte data (e.g. the uint8 vector in the example above).

This function is a modified version of the urlread.m function that is part of Matlab R2010a. Also included is the stock urlreadwrite.m helper function since this is used by urlreadpost.m, but won't be found in its private location by default.
 

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
23 Aug 2011 R

Very cool code, partly written in java.
Does what it promises: enables file uploads to a web service.
I found this code even before I discovered that the regular urlread cannot do this. Love it! Used with R2010b and php/apache server.

Please login to add a comment or rating.
Updates
08 Apr 2010

I fixed a typo in the example usage fragment (d was referred to as dd).

Tag Activity for this File
Tag Applied By Date/Time
http Dan Ellis 08 Apr 2010 10:58:05
url Dan Ellis 08 Apr 2010 10:58:05
post Dan Ellis 08 Apr 2010 10:58:05
binary Dan Ellis 08 Apr 2010 10:58:05
www Dan Ellis 08 Apr 2010 10:58:05
data import Dan Ellis 08 Apr 2010 10:58:05
file Dan Ellis 08 Apr 2010 10:58:05
http Jan Tore Korneliussen 09 Apr 2010 10:48:43

Contact us at files@mathworks.com