Oxford Emotion Api

by Leonardo Trimarchi

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
 <p>This example shows how to post an image to the Project Oxford Emotion API using a binary file</p>
 <input type="file" id="filename" name="filename">
 <button id="btn">Click here</button>
 <p id="response"></p>

JavaScript

var apiKey = "1dd1f4e23a5743139399788aa30a7153";
 
 //apiUrl: The base URL for the API. Find out what this is for other APIs via the API documentation
 var apiUrl = "https://api.projectoxford.ai/emotion/v1.0/recognize";
 
 $('#btn').click(function () {
 //file: The file that will be sent to the api
 var file = document.getElementById('filename').files[0];
 
 CallAPI(file, apiUrl, apiKey);
 });
 
 function CallAPI(file, apiUrl, apiKey)
 {
 $.ajax({
 url: apiUrl,
 beforeSend: function (xhrObj) {
 xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
 xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
 },
 type: "POST",
 data: file,
 processData: false
 })
 .done(function (response) {
 ProcessResult(response);
 })
 .fail(function (error) {
 $("#response").text(error.getAllResponseHeaders());
 });
 }
 
 function ProcessResult(response)
 {
 var data = JSON.stringify(response);
 $("#response").text(data);
 }