HTTP Upload

Description

This application note describes the procedure for using the http_upload command.

Setup

This demonstration uses a Zentri Moray evaluation board, but it works for any board with a ZentriOS device. This assumes the module is configured the default factory settings.

Open a ZentriOS terminal to the module and configure the module to connect to your local network as follows:

set wlan.ssid               <YOUR SSID>
set wlan.passkey            <YOUR PASSWORD>
set wlan.auto_join.enabled  1
save
reboot

In an application that uses the http_upload command, a server is required to process the POST requests generated by the command.

Download the file upload server python script: file_upload_server.py (Python 2.7).

This HTTP server responds to file upload POST requests by saving the file in its working directory. The server also outputs details of each request on stdout.

Copy the downloaded script to a directory on your server computer.

Run the server from the directory containing the script. The server computer must be connected to the same network as the ZentriOS device. The output is similar to the following:

> file_upload_server.py
file_upload_server.py
You can specify server port as an argument, e.g. file_upload_server.py 8003. Default port:8000
Serving at: http://localhost:8000
<Server IP>: 10.5.6.60

Note the local IP address of the computer running the server. Depending on the server computer configuration, the python script may display the local IP address correctly (shown as 10.5.6.60 above). However if 127.0.0.1 or similar is displayed, use another method to obtain the server local IP address. This needs to be substituted for the symbol <Server_IP> in the ZentriOS command sequences below.

In the example output below:

Creating Test Files

First create two test files on your module.

In the ZentriOS terminal, enter the following command sequence:

ZentriOS CommandsDescription

fcr test1.txt 9
test1.txt
fcr test2.txt 9
test2.txt

Create test1.txt
Content:test1.txt
Create test2.txt
Content:test2.txt

Uploading a Single File

The simplest way to use http_upload command is to upload a single file.

In the ZentriOS terminal, enter the following command sequence:

ZentriOS CommandsDescription

http_upload <Server IP>:8000 test1.txt

Upload a single file to the test server

The file_upload_server.py output is similar to the following:

Successful Upload:
File:C:\http_upload\test1.txt
from host:10.5.6.70 port:15294
10.5.6.70 - - [30/Sep/2015 15:57:11] "POST / HTTP/1.1" 200 -

The file has been uploaded to the server script directory.

The ZentriOS terminal output is similar to the following:

Uploading: test1.txt to server
Request POST /
Connecting (http): 10.5.6.60:8000
HTTP response: 200
Success

Uploading a Single File with a Different Remote File Name

You can specify a remote file name that is different to the original file name on the module.

In the ZentriOS terminal, enter the following command sequence:

ZentriOS CommandsDescription

http_upload <Server IP>:8000 test1.txt testOne.txt

Upload a single file to the test server

The file_upload_server.py output is similar to the following:

Successful Upload:
File:C:\http_upload\testOne.txt
from host:10.5.6.70 port:20295
10.5.6.70 - - [30/Sep/2015 16:08:21] "POST / HTTP/1.1" 200 -

The ZentriOS terminal output is the same as for the previous example.

Uploading Multiple Files

You can upload multiple files with a single command using a manifest. The simplest manifest specifies only the server and the file names, for example:

{
  "path" : "http://10.5.6.60:8000",
  "files" : [ 
    {
    "local"  : "test1.txt",
    },
    {
    "local"  : "test2.txt",
    },
  ]
}

The manifest is in json format. You cannot include comments in the json.

The length of the above manifest is 140 characters, assuming the manifest line endings are <LF>, not <CR><LF>. Determine the length depending on the system you use to prepare your manifest.

In the ZentriOS terminal, enter the following command sequence:

ZentriOS CommandsDescription

http_upload -m 140
{
  "path" : "http://10.5.6.60:8000",
  "files" : [ 
    {
    "local"  : "test1.txt",
    },
    {
    "local"  : "test2.txt",
    },
  ]
}

Upload multiple files specifying manifest length of 140
Directly after issuing the command, 
paste the manifest text into the ZentriOS terminal. 










The file_upload_server.py output is similar to the following:

Successful Upload:
File:C:\http_upload\test1.txt
from host:10.5.6.70 port:40299
10.5.6.70 - - [30/Sep/2015 16:22:09] "POST / HTTP/1.1" 200 -
Successful Upload:
File:C:\http_upload\test1.txt
from host:10.5.6.70 port:45300
10.5.6.70 - - [30/Sep/2015 16:22:09] "POST / HTTP/1.1" 200 -

The files have been uploaded to the server script directory.

The ZentriOS terminal output is similar to the following:

Uploading: test1.txt to server
Request POST /
Connecting (http): 10.5.6.60:8000
HTTP response: 200
Uploading: test2.txt to server
Request POST /
Connecting (http): 10.5.6.60:8000
HTTP response: 200
Success

Uploading Multiple Files Specifying Remote Names and Mime Types

To specify remote names and mime types, add the required properties according to the description in http_upload. For example,

In the ZentriOS terminal, enter the following command sequence:

ZentriOS CommandsDescription

http_upload -m 311
{
  "path" : "http://10.5.6.60:8000",
  "files" : [ 
    {
    "local"  : "test1.txt",
    "remote" : "FirstTest.txt",
    "name"   : "file",
    "type"   : "text/plain"
    },
    {
    "local"  : "test2.txt",
    "remote" : "SecondTest.txt",
    "name"   : "file",
"type" : "text/plain" }, ] }

Upload multiple files specifying manifest length of 311
Directly after issuing the command, 
paste the manifest text into the ZentriOS terminal. 
















Note that the "name" property is set to the default value of "file". The file_upload_server.py script assumes the form-data name property is set to file.

The file_upload_server.py output is similar to the following:

Successful Upload:
File:C:\http_upload\FirstTest.txt
from host:10.5.6.70 port:36013
10.5.6.70 - - [30/Sep/2015 17:06:30] "POST / HTTP/1.1" 200 -
Successful Upload:
File:C:\http_upload\SecondTest.txt
from host:10.5.6.70 port:41014
10.5.6.70 - - [30/Sep/2015 17:06:30] "POST / HTTP/1.1" 200 -

The files have been uploaded to the server script directory.

The ZentriOS terminal output is the same as for the previous example.

Change Log

ModifiedChangesZentriOS Version
Required
2015-09-30Created1.0+