Using the Alexa voice service RestAPI with cURL - rest

I'd like to the the Alexa voice API (https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/rest/speechrecognizer-requests) with curl. The voicerecogniser API call is more complex than I'm used to using and needs an MP3 file attaching that includes the voice sample. Can anyone advise on how the following would be structured with curl? (There's more info at the given link)
POST /v1/avs/speechrecognizer/xxxxxxxxxxxx HTTP/1.1
Host: access-alexa-na.amazon.com
Authorization: Bearer xxxxxxxxxxxx
Content-Type: multipart/form-data; boundary=boundary_term
Transfer-Encoding: chunked
--boundary_term
Content-Disposition: form-data; name="request"
Content-Type: application/json; charset=UTF-8
{
"messageHeader": {
"deviceContext": [
{
"name": "playbackState",
"namespace": "AudioPlayer"
"payload": {
"streamId": "xxxxxxxxxxxx",
"offsetInMilliseconds": "xxxxxxxxxxxx",
"playerActivity": "xxxxxxxxxxxx"
}
},
{
...
},
...
]
},
"messageBody": {
"profile": "alexa-close-talk",
"locale": "en-us",
"format": "audio/L16; rate=16000; channels=1"
}
}
--boundary_term
Content-Disposition: form-data; name="audio"
Content-Type: audio/L16; rate=16000; channels=1
...encoded_audio_data...
--boundary_term--

I'm no bash expert but this how I was able to interact with AVS using cURL. I generate a file containing the multipart body content which includes the binary audio data and pass that along to cURL.
############################################################
# First we creat a bunch of variables to hold data.
############################################################
# Auth token
TOKEN="Atza|IQEBLjAsAhR..."
# Boundary
BOUNDARY="BOUNDARY1234"
BOUNDARY_DASHES="--"
# Newline characters
NEWLINE='\r\n';
# Metadata headers
METADATA_CONTENT_DISPOSITION="Content-Disposition: form-data; name=\"metadata\"";
METADATA_CONTENT_TYPE="Content-Type: application/json; charset=UTF-8";
# Audio headers
AUDIO_CONTENT_TYPE="Content-Type: audio/L16; rate=16000; channels=1";
AUDIO_CONTENT_DISPOSITION="Content-Disposition: form-data; name=\"audio\"";
# Metadata JSON body
METADATA="{\
\"messageHeader\": {},\
\"messageBody\": {\
\"profile\": \"alexa-close-talk\",\
\"locale\": \"en-us\",\
\"format\": \"audio/L16; rate=16000; channels=1\"\
}\
}"
############################################################
# Then we start composing the body using the variables.
############################################################
# Compose the start of the request body
POST_DATA_START="
${BOUNDARY_DASHES}${BOUNDARY}${NEWLINE}${METADATA_CONTENT_DISPOSITION}${NEWLINE}\
${METADATA_CONTENT_TYPE}\
${NEWLINE}${NEWLINE}${METADATA}${NEWLINE}${NEWLINE}${BOUNDARY_DASHES}${BOUNDARY}${NEWLINE}\
${AUDIO_CONTENT_DISPOSITION}${NEWLINE}${AUDIO_CONTENT_TYPE}${NEWLINE}"
# Compose the end of the request body
POST_DATA_END="${NEWLINE}${NEWLINE}${BOUNDARY_DASHES}${BOUNDARY}${BOUNDARY_DASHES}${NEWLINE}"
# Now we create a request body file to hold everything including the binary audio data.
# Write metadata to body file
echo -e $POST_DATA_START > multipart_body.txt
# Append binary audio data to body file
cat hello.wav >> multipart_body.txt
# Append closing boundary to body file
echo -e $POST_DATA_END >> multipart_body.txt
############################################################
# Finally we get to compose the cURL request command
# passing it the generated request body file as the multipart body.
############################################################
# Compose cURL command and write to output file
curl -X POST \
-H "Authorization: Bearer ${TOKEN}"\
-H "Content-Type: multipart/form-data; boundary=${BOUNDARY}"\
--data-binary #foo.txt\
https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize\
> response.txt
The audio MUST be mono channel, sampled at 16k Hz, and signed 16 bit PCM. Otherwise AVS sends nothing back.
For more information check out my Alexa Voice Service (AVS) with cURL blog post.

Related

vscode-restclient How to process / parse response body

The vscode extension vscode-restclient allows to create http request and handle the response similar to curl or postman.
A POST request to /sales/getResult/ returns this response
HTTP/1.1 200 OK
Date: ....
Content-Type: text/plain; charset=utf-8
Content-Length: 67
Connection: close
Load your results with ID: CJoYTvh8
From the body we need the id CJoYTvh8 to get details. The request to get details goes to the URL /sales/GetResult/{{resultId}} . Where {{resultId}} stands for the ID CJoYTvh8 from the previous response body.
I want to create the request for the details which needs to look like this
#resultId = {{myRequest.response.body}} // this should only be CJoYTvh8
# #name getResult_for_id
POST /sales/getResult/{{resultId}} HTTP/1.1
Host: {{our_host}}
Authorization: Bearer {{authToken}}
Content-Type: application/json
I am looking for something like this
#resultId = response.body.split(':')[1].trim()
Question
How can i split the string Load your results with ID: CJoYTvh8 in vscode-restclient so that i assign only the id CJoYTvh8 to the variable #resultId?

Set Content Type for Specific MultiPart in rest assured

I am using 2 multiparts ( 1 is a file the other is a form-data )
For the file I am using the default content type but (multipart/form-data) but for the other I need to use application/json
I don't know how to set content type for that specific multipart/fpr, data ===
This is what I have done -
Response resp = RestAssured.given().baseUri(UPLOAD)
.header("Content-Type", "multipart/form-data; boundary=--abcd")
.multiPart("files",new File(System.getProperty("user.dir") +picPath))
.multiPart("JSON_DATA",JSON_DATA )
.post().then().extract().response();
But this is the request that has been generated --
Content-Disposition: form-data; boundary=--abcd; name = files; filename =Photo.png
Content-Type: application/octet-stream
/../../../Photo.png
------------
Content-Disposition: form-data; boundary=--abcd; name = JSON_Data; filename = file
**Content-Type: text/plain**
{.......}
HOW DO I SET THE 2ND ONE TO application/json instead of text/plainm
You can define content-type for each part like this, no need put form-data in header
given().log().all()
.multiPart("file", file)
.multiPart("JSON_DATA", JSON_DATA, "application/json")
.post();

Titanium multipart file upload with different content types?

So I am trying to post to a Salesforce.com REST api that is expecting the following sample request -
POST /services/data/v33.0/chatter/feed-elements HTTP/1.1
Authorization: OAuth 00DRR0000000N0g!...
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: instance_name
Content-Length: 845
Content-Type: multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Accept: application/json
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
{
"body":{
"messageSegments":[
{
"type":"Text",
"text":"Please accept this receipt."
}
]
},
"capabilities":{
"content":{
"description":"Receipt for expenses",
"title":"receipt.pdf"
}
},
"feedElementType":"FeedItem",
"subjectId":"005RR000000DmOb"
}
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="feedElementFileUpload"; filename="receipt.pdf"
Content-Type: application/octet-stream; charset=ISO-8859-1
...contents of receipt.pdf...
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq--
As you can see, the two parts of the request is expecting different content types.
In Titanium, I have the following code -
xhr.open("POST", postUri);
xhr.setRequestHeader("Authorization", authHeader);
xhr.setRequestHeader('enctype', 'multipart/form-data');
var data2send = {
json: jsonObj,
feedElementFileUpload: imageBlob
};
xhr.send(data2send);
According to the Appcelerator documentation and a couple of online threads, the httpclient should be able to set each part automatically, however, Salesforce API still gives an error message of:
[{"errorCode":"MISSING_ARGUMENT","message":"Missing required 'subjectId' parameter."}]
I can confirm that subjectId is in the jsonObj and its content is correct. It seems that the json part is not recognised as JSON by Salesforce correctly. How can I set the content-type correctly? Many thanks.
Updates - I used the Wireshark to capture the request text. Apparently the httpclient did not set up the content-type for the json part that Salsforce API is expecting. Any idea how to set it?
POST /services/data/v33.0/chatter/feed-elements HTTP/1.1
Host: mydomain.com
Accept-Language: en-us
User-Agent: Appcelerator Titanium/3.5.1 (iPhone Simulator/8.2; iPhone OS; en_US;)
enctype: multipart/form-data
X-Requested-With: XMLHttpRequest
Accept: */*
Content-Type: multipart/form-data; charset=utf-8; boundary=0xTibOuNdArY_1430987526
Connection: keep-alive
Authorization: Bearer $My_AUTH_CODE
X-Titanium-Id: 8329dd1d-3379-4d7c-955e-120ad1586a2b
Content-Length: 93333
Accept-Encoding: gzip, deflate
--0xTibOuNdArY_1430987526
Content-Disposition: form-data; name="json"
{"feedElementType":"FeedItem","subjectId":"xxxxxxxxxx","body":{"messageSegments":[{"type":"text","text":"test"}]},"capabilities":{"content":{"description":"sdfaadfs","title":"adsfafsd.png"}}}
--0xTibOuNdArY_1430987526
Content-Disposition: form-data; name="feedElementFileUpload"; filename="01430987526.png"
Content-Type: image/png
my file data...

REST interface using nginx httpluamodule

I'm just getting started with nginx and the HttpLuaModule. I've created a test configuration file to familiarize myself with how things work.
now I'm trying to write some logic to accept GET, POST and DELETE requests for a specific type of resource.
I would like to create a "location" entry that would match the following URI / accept the following curl calls:
curl -i -X GET http://localhost/widgets/widget?name=testname&loc=20000 -H "Accept:application/json"
This is what my current nginx.conf looks like:
server {
listen 80;
server_name nsps2;
root /var/www/;
index index.html index.htm;
#charset koi8-r;
#access_log logs/host.access.log main;
#curl http://localhost/hello?name=johndoe
location /hello {
default_type "text/plain";
content_by_lua '
local rquri = ngx.var.request_uri;
ngx.say("the uri is ", rquri ,".")
local name = ngx.var.arg_name or "Anonymous"
ngx.say("Hello, ", name, "!")
';
}
location / {
root /var/www/;
index index.html index.htm;
}
#curl -i -X GET http://localhost/widgets/widget?name=testname&loc=20000 -H "Accept:application/json"
location /widgets/widget {
root /var/www/widgets;
default_type "text/pain";
content_by_lua '
local arga,argb = ngx.arg[1], ngx.arg[2] ;
ngx.say("the arga is ", arga ,".")
ngx.say("the argb is ", argb, ".")
';
}
Using the last "location" entry, I'm trying to
1. prove that the system is getting the GET request
2. prove that I understand how to access the parameters passed in with the GET request.
I'm getting an error right now that looks like this:
2015/02/24 20:18:19 [error] 2354#0: *1 lua entry thread aborted: runtime error: content_by_lua:2: API disabled in the context of content_by_lua*
stack traceback:
coroutine 0:
[C]: in function '__index'
content_by_lua:2: in function <content_by_lua:1>, client: 127.0.0.1, server: nsps2, request: "GET /widgets/widget?name=testname?loc=20000 HTTP/1.1", host: "localhost"
I'm not too sure about what this error means / is trying to tell me.
Any tips would be appreciated.
Thank you.
EDIT 1
I think the problem was something syntactically wrong. I was reading the manual and found an example to try. I've changed the code to look like this:
location /widgets/widget {
default_type "text/pain";
content_by_lua '
local args = ngx.req.get_uri_args()
for key, val in pairs(args) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
';
}
Now when I call the app like this:
mytestdevbox2:/var/www/nsps2# curl -i -X GET http://localhost/widgets/widget?name=testname&loc=20000 -H "Accept:application/json"
-ash: -H: not found
mytestdevbox2:/var/www/nsps2# HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Tue, 24 Feb 2015 21:32:44 GMT
Content-Type: text/pain
Transfer-Encoding: chunked
Connection: keep-alive
name: testname
[1]+ Done curl -i -X GET http://localhost/widgets/widget?name=testname
After the system displays the "name: testname" stuff, it just sits there until I hit "enter". After I do that, then it proceeds to display the 1+ stuff.
I'm not too sure what it's doing.
EDIT 2:
Adding quotes to the curl call did fix the problem:
mytestdevbox2:/var/www/nsps2# curl -i -X GET 'http://localhost/widgets/widget?name=testname&loc=20000'
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 25 Feb 2015 12:59:29 GMT
Content-Type: text/pain
Transfer-Encoding: chunked
Connection: keep-alive
loc: 20000
name: testname
mytestdevbox2:/var/www/nsps2#
The problem described in EDIT 1 doesn't have anything to do with your question about nginx and is caused by not quoting the parameters for curl when you execute the command:
curl -i -X GET http://localhost/widgets/widget?name=testname&loc=20000
This is executed as two commands separated by '&': curl -i -X GET http://localhost/widgets/widget?name=testname and loc=20000; that's why you see the output after you already got the prompt back as the first command is now executed in the background. "1+ Done" message is a confirmation that the background process is terminated; it's just shown after you press Enter.
Wrap the URL with the query string in quotes and you should see the expected behavior.

Getting 500 error (missing ;) from blobstore upload

I would like to skip the part where an upload URL is sent to the client, and upload directly to the blobstore from the backend. I use this to send the multipart request, although I get:
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing/_ah/upload/ahB0NTIzNjU4OTY1ODk1Njg5ciILEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18YgICAgICAiAs M. Reason:
<pre> Missing ';'</pre></p><h3>Caused by:</h3> <pre>javax.mail.internet.ParseException: Missing ';'
at javax.mail.internet.ParameterList.<init>(ParameterList.java:135)
at javax.mail.internet.ContentType.<init>(ContentType.java:72)
at javax.mail.internet.MimeMultipart.<init>(MimeMultipart.java:98)
at com.google.apphosting.utils.servlet.MultipartMimeUtils.parseMultipartRequest(MultipartMimeUtils.java:41)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.handleUpload(UploadBlobServlet.java:173)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.access$000(UploadBlobServlet.java:71)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet$1.run(UploadBlobServlet.java:117)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.doPost(UploadBlobServlet.java:114)
this is the data sent:
--__END_OF_PART__
Content-Type: image/bmp
content-transfer-encoding: binary
content-disposition: form-data; name="file"
[binary string here]
--__END_OF_PART__--
these are the headers:
accept-encoding: gzip,
content-type: multipart/form-data; boundary=__END_OF_PART__
and this is the code:
BlobstoreService service = BlobstoreServiceFactory.getBlobstoreService();
String url = service.createUploadUrl("/upload");
HttpRequestFactory factory = UrlFetchTransport.getDefaultInstance().createRequestFactory();
MultipartFormContent content = new MultipartFormContent();
content.addPart(new MultipartFormContent.Part(
"file",
new InputStreamContent("image/bmp",
new ByteArrayInputStream(Base64.decodeBase64(data)))));
content.writeTo(System.out);
HttpRequest request = factory.buildPostRequest(new GenericUrl(new URL(url)), content);
request.getHeaders().setContentType(content.getMediaType().build());
request.execute();
As noted in the docs, the multipart body has to be multipart/form-data with input type "file", which produces the Content-Disposition header with "filename" section:
Content-Disposition: form-data; name="myFile"; filename="imagename.bmp"
With HttpClient it should be something like this.