HTTP Upload
Description
This application note describes the procedure for using the http_upload command.
Setup
This demonstration works for any board with a Gecko OS device. This assumes the module is configured the default factory settings.
Open a Gecko OS 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 Gecko OS 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 Gecko OS command sequences below.
In the example output below:
-
the Server IP and port are
10.5.6.60:8000
-
the Gecko OS device IP is
10.5.6.70
-
the server script is in the
C:\http_upload
directory on a Windows computer.
Creating Test Files
First create two test files on your module.
In the Gecko OS terminal, enter the following command sequence:
Gecko OS Commands | Description |
---|---|
|
|
Uploading a Single File
The simplest way to use http_upload command is to upload a single file.
In the Gecko OS terminal, enter the following command sequence:
Gecko OS Commands | Description |
---|---|
|
|
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 - - [01/Jan/2019 15:57:11] "POST / HTTP/1.1" 200 -
The file has been uploaded to the server script directory.
The Gecko OS 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 Gecko OS terminal, enter the following command sequence:
Gecko OS Commands | Description |
---|---|
|
|
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 - - [01/Jan/2019 16:08:21] "POST / HTTP/1.1" 200 -
The Gecko OS 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 Gecko OS terminal, enter the following command sequence:
Gecko OS Commands | Description |
---|---|
|
|
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 - - [01/Jan/2019 16:08:21] "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 - - [[01/Jan/2019 16:08:21] "POST / HTTP/1.1" 200 -
The files have been uploaded to the server script directory.
The Gecko OS 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 Gecko OS terminal, enter the following command sequence:
Gecko OS Commands | Description |
---|---|
|
|
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 - - [01/Jan/2019 16:08:21] "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 - - [01/Jan/2019 16:08:21] "POST / HTTP/1.1" 200 -
The files have been uploaded to the server script directory.
The Gecko OS terminal output is the same as for the previous example.
Supporting Gecko OS Versions
- Gecko OS 4
Change Log
Modified | Changes |
---|---|
2019-01-01 | Created |