Slog Data via HTTP POST - SensorBase.org
From CSL Wiki
Once you have (1) created a project and (2) created project tables, you can slog data through the interface, through an HTTP POST or through using SOAP. Here is how to slog data via an HTTP POST.
There are six bits of information that need to be provided to successfully slog your data:
- email - The email that you use to login to SensorBase.org. If you do not have an account, contact the system admin to get one.
- pw - The password that you use to login to SensorBase.org. If you do have an account, contact the system admin to get one.
- project_id - All projects you create are given a project_id, which can be found on a project's summary page. You can find a list of your projects in My Account where there are links to your projects' summary pages.
- data_string - This is your string of actual data, which must follow the SensorBase.org XML format.
- type - Specify the type of your data_string as 'xml'.
- tableName - Specify the name of the table that you are slogging to.
Contents |
[edit]
Examples
There are several ways to perform an HTTP POST, and SensorBase alpha accepts uploads as an XML string or an XML file. Below are a couple of examples using Python and Curl.
NOTE: When you are slogging an XML file, it MUST be in the SensorBase.org XML format.
[edit]
Uploading XML String via Python
""" upload xml string to a table in sensorbase.
project id: 31
table name: newtable1
field name: Field_Name1, Field_Name2
two rows are uploaded in this xml string."""
import urllib
import urllib2
sb_email = 'your_email'
sb_password = 'your_password'
sb_project_id = '31'
sb_table = 'newtable1'
xml = '<table><row><field name="Field_Name1">value11</field><field name="Field_Name2">value21</field></row><row><field name="Field_Name1">value10</field><field name="Field_Name2">value20</field></row></table>'
sb_api = 'http://sensorbase.org/alpha/upload.php' # the interface of sensorbase used for uploading data
param = {'email' : sb_email,
'pw' : sb_password,
'project_id' : sb_project_id,
'data_string':xml,
'type':'xml',
'tableName':sb_table}
data = urllib.urlencode(param)
req = urllib2.Request(sb_api, data)
response = urllib2.urlopen(req)
print "DATA POST result: " + response.read()
response.close()
[edit]
Uploading XML File via Curl
curl --connect-timeout 10 --max-time 120 -s -S \ --form 'email=EMAIL_HERE' --form 'pw=PASSWORD_HERE' \ --form 'project_id=22' --form 'tableName=demoData' \ --form 'data_file=@path/to/data.xml' --form 'type=xml' \ http://sensorbase.org/alpha/upload.php
[edit]
Uploading CSV File via Curl
curl --connect-timeout 10 --max-time 120 -s -S \ --form 'email=EMAIL_HERE' --form 'pw=PASSWORD_HERE' \ --form 'project_id=22' --form 'tableName=demoData' \ --form 'data_file=@path/to/data.csv' --form 'type=csv' \ http://sensorbase.org/alpha/upload.php
[edit]
Uploading Image Files via Perl (Courtesy of Shaun Ahmadian)
(note: datetime, location_id and image are fields of the table defined earlier from sensorbase user interface)
#!/usr/bin/perl
use strict;
my $dir=$ARGV[0];
my $url="http://sensorbase.org/alpha/upload.php";
my $project_id=YOUR_PROJECT_ID;
my $sensorbase_user='********@******';
my $sensorbase_password='********';
foreach my $imageFile (<$dir/*>) {
if ($imageFile=~/mote(\d+)_date\d+_(\d{4})_(\d{2})_(\d{2})_(\d{2})_
(\d{2})/){
my($mote_id, $year,$mon,$day, $hour,$min)=($1,$2,$3,$4,$5,$6);
if ($year<2004)
{ next; } # We don't want 1970!
my $datetime="$year-$mon-$day $hour:$min:00";
my $CURL="curl --connect-timeout 10 --max-time 120 -s -S --form
'email=$sensorbase_user' --form 'pw=$sensorbase_password' --form
'project_id=$project_id' --form 'tableName=JR_Experiment_2' --form
'MAX_FILE_SIZE=2097152' --form 'blob=1' --form 'datetime=$datetime' --
form 'location_id=$mote_id' --form 'image=\@$imageFile' $url";
my $result=`$CURL 2>&1`;
my $error=`echo $?`;
print $CURL;
if ($error > 0){
print STDERR "File: $imageFile : $error \n";
die "Executing of $CURL failed:\n$result\n";
}
print "RESULT: $result\n";
}
}
