所以我正在将视频发布到Emotion API进行视频发布,但一直无法得到响应。
我已经能够在微软在线控制台上运行它,但当我试图使用(1)JavaScriptAjax或(2)Ruby服务器端代码在Rails应用程序中实现它时,我总是会遇到各种错误。
这是我的密码。起初我尝试使用Ajax方式,但我怀疑API没有启用CORS。于是我尝试了Ruby,但没有成功。
Ruby尝试:
def index
uri = URI('https://api.projectoxford.ai/emotion/v1.0/recognizeinvideo')
uri.query = URI.encode_www_form({
})
data = File.read("./public/mark_zuck.mov")
request = Net::HTTP::Post.new(uri.request_uri)
# Request headers
request['Ocp-Apim-Subscription-Key'] = 'e0ae8aad4c7f4e33b51d776730cff5a9'
# Request body
request.body = data
request.content_type = "video/mov"
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
puts response.body
end
这是我的Ajax尝试:
function CallAPI(apiUrl, apiKey){
console.log("API called");
$(".loading").css("display", "inline-block");
$.ajax({
url: apiUrl,
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
},
type: "POST",
data: '{"url": "http://localhost:5000/mark_zuck.mov"}',
processData: false,
success: function(response){
console.log("API success");
ProcessResult(response);
$(".loading").css("display", "none");
console.log(response);
},
error: function(error){
console.log("API failed");
$("#response").text(error.getAllResponseHeaders());
$(".loading").css("display", "none");
console.log(error);
}
})
是的,我重新生成了我的钥匙。这只是为了说明我的观点。
因此,如果要发送的是二进制文件,则必须将Content-Type
设置为application/octet流,就像我一样。
如果使用url,则应将Content-Type
设置为application/json,并且该url必须公开。