Matlab: using webwrite to send image through a simple REST API - matlab

I was trying to do image classification through a web at service at and
I had no problem to use curl to retrieve token:
curl -i -X POST -H "Authorization:MY Key" -F "image_request[image]=#/path/to/myimage" -F "image_request[locale]=en-US" https://api.cloudsightapi.com/image_requests
However, it is not successful when I tried webwrite, it returned "HTTP 400" error.
option=weboptions('KeyName','Authorization','KeyValue','mykey')
fid = fopen('/path/to/myimage');
img = fread(fid,Inf,'*uint8');
fclose(fid);
response=webwrite('https://api.cloudsightapi.com/image_requests',...
'image_request[image]',img,...
'image_request[locale]','en-US',option);
I guess it because the function webwrite in this format doesn't support "multipart/form-data" and I need change the media type. Then I tried to send data as an JSON object
option=weboptions('KeyName','Authorization','KeyValue','mykey','MediaType','application/json')
data=struct('image_request[image]',im,'image_request[locale]','en-US');
response=webwrite('https://api.cloudsightapi.com/image_requests',data,option)
But the field name in Matlab struct does not allow "[".
Any suggestion?

Related

Seed in not valid waves-node

I set up my node on the waves test network.
Created a wallet with a POST request
curl -X 'POST'
'http://127.0.0.1:6869/addresses'
Now I want to get SEED to access the wallet with a request
curl -X 'GET'
'http://127.0.0.1:6869/addresses/seed/3Mq9Uim6xMZjfn5z3ckMKZyb7FdKvJYjrUj'
But the resulting seed is not valid.
Do I need to convert it somehow?
enter image description here

Curl, How to send list of strings using GET request?

I want to send ['1', '2', '3'] as a GET request.
I thought GET request are used when you are retrieving data opposed to POST (when you are modifying/creating data)
Failing to google how to send list of strings with GET does make me wonder, if it's better to use POST here?
Once you intend to perform a GET request, you could send data in the query string using one of the following approaches:
curl -G http://example.org -d "query=1,2,3"
curl -G http://example.org -d "query=1&query=2&query=3"
Let me highlight that payloads in GET requests are not recommended. Quoting the RFC 7231:
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
Also bear in mind that GET requests shouldn't be used to modify resources: they are intended to be used for information retrieval only, without side effects. Having said that, GET is both safe and idempotent. You can see more details on these concepts in this answer.
If the data must be sent in the payload (and you intend to modify the resource) then stick to POST. Assuming your payload is a JSON document, you would have something like:
curl -X POST http://example.org \
-H "Content-Type: application/json" \
-d '["1", "2", "3"]'
If you want to send it in the body with curl you can call your service like this:
curl -X GET --data "['1', '2', '3']" "https://example.com/test.php"
For example in PHP you can read it from the read-only stream php://input
<?php
$get_body = file_get_contents('php://input');
A better way would be to assign the array to a parameter e.g. x:
curl -X GET "https://example.com/test.php?x[]=1&x[]=2&x[]=3"
In PHP you'll receive these values as an array in $_GET['x']:
<?php
print_r($_GET['x']);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)

How to run h2o AutoML by using rest api?

I want run AutoML in h2o by using rest api? I know the url is /99/AutoMLBuilder. But I have no idea that how can I send the parameters. There is no sample code on the official web site. I can access model import/export by using curl because the parameters are flat. But it seems that maybe the parameters of AutoML are nested, and I cannot find any sample code or answers about the format of the parameters.
The parameters are sent in JSON format through a POST command. For example, assuming a training frame named airlines has already been loaded, you can train an AutoML model through curl with:
curl -X POST http://localhost:54321/99/AutoMLBuilder -H "Content-Type: application/json" -d '{"input_spec": {"training_frame":"airlines", "response_column":"IsDepDelayed"}, "build_control": {"project_name":"aml_curl_test", "stopping_criteria":{"max_models":3} } }'
You can find the full REST API reference here: http://docs.h2o.ai/h2o/latest-stable/h2o-docs/rest-api-reference.html, which will tell you the JSON object names each parameter belongs to.

Difficulty POSTing multipart/form-data in tcl rest

I'm trying to POST an image in tcllib's rest package as part of a multipart/form-data document. I believe I just need to format the payoad properly.
The url looks like:
POST /api/v1/rawImage/1000?slice=1
I can do this easily with curl (and other things as well) with:
curl -v -X POST -H "Content-Type: multipart/form-data" -F imageFile=#../../images/data/Image_1.bin http://${HOST}/api/v1/rawImage/1000?slice=1
In looking through rest.tcl, I don't see anything that explictly formats the boundaries for the payload.
Here is what I have to try the POST:
#!/usr/bin/tclsh
package require rest
package require json
# pull in Image data
set fh [open "Image_1.bin"]
fconfigure $fh -encoding binary -translation lf
set filedata [read $fh]
close $fh
puts "filedata length: [string length $filedata]"
# POST request
set url http://localhost:5007/api/v1/rawImage/100?slice=1
set header [list content-type multipart/form-data]
set config [list format json method post headers $header]
set form_data {rawImage $filedata}
set res [::rest::simple $url {} $config $form_data]
puts $res
The following HTTP header is seen wireshark:
You could take a look to the create interface command documentation, the example of Google Docs.
Also, if you want to know more about the mime boundary string, you also could see the proc mime_multipart in the source code of "rest".

How to use Matlab webread with 2 http headers

I need to do this GET call with matlab:
curl -X GET \
-H "X-Parse-Application-Id: my_AppKEY" \
-H "X-Parse-REST-API-Key: my_APIKEY" \
https://api.parse.com/1/classes/GameScore
(this is a basic request to parse.com)
In matlab 2014b I have the function webread which becomes a weboptions struct.
I think the correct way of setting a header for the request is like this:
myHeaders1 = weboptions('KeyName', 'X-Parse-Application-Id', 'KeyValue' , 'my_KEY')
The thing is... I can only set 1 header using this syntax. How can I set 2 or more headers to be used in webread?
You can either call curl from within Matlab if you load right library files or instead you can use urlread2