Leveraging Emotion API in Video Content (JavaScript or Ruby)

Currently, I'm in the process of uploading a video to the Emotion API for videos, but unfortunately, I have not received any response yet.

I managed to successfully upload it using the Microsoft online console. However, my attempts to integrate it into my Rails app via either (1) JavaScript Ajax or (2) Ruby server-side code have been met with various errors consistently.

Initially, I attempted the Ajax method, suspecting that the API might not have CORS enabled. Subsequently, I tried the Ruby approach but encountered no success.

Ruby code attempt:

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 code attempt:

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);
        }
    })

Yes, I've already regenerated my key. This example was solely meant to showcase my situation.

Answer №1

When sending a binary file, such as I did, remember to specify the Content-Type as application/octet-stream.

If you are using a URL, make sure to set the Content-Type to application/json and ensure that the URL is accessible to the public.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtaining the pathname in a NextJS file like _document.js is a matter of accessing

I'm looking to retrieve the current URL path in my /page/_document.js file. I've created a class and my goal is to implement a conditional statement based on this value. Below is the code snippet (similar to the example provided in NextJS docume ...

What could be the reason for the Express function Router() returning a value of undefined?

Currently, I am working with TypeScript and Express to develop an API that adheres to the principles of Clean Architecture. To organize my application, I have structured each route in separate folders and then imported them all into an index.ts file where ...

Guide on creating and synchronizing an XML/JSON file that stores beat information (BPM) for audio using JavaScript

Looking to sync Javascript events with the BPM of music for a game similar to "Guitar Hero." We start with: In order to create a track file based on beat detection (with each BPM saved like sheet music), how can it be generated beforehand rather than in ...

Facing issues with Express, http-proxy-middleware, and encountering the error net::ERR_CONNECTION_REFUSED

For some time now, I've been troubleshooting an issue with my Express App that utilizes http-proxy-middleware to forward requests to another backend service. The problem arises when a third-party application makes a request to my server using an IP ad ...

Angular Validation displays ng-valid when the form is actually invalid

I am currently working on a wedding RSVP form https://i.stack.imgur.com/Ct8Ux.png My objective is to hide the DONE submit button and only display it when the form is considered valid. <form method="POST" action="http://l.bheng.com:8888/wedding" acce ...

Adding fadeIn effect to a prepended element using jQuery

I am struggling with this piece of code: $.ajax({ url : url, data : {ids : JSON.stringify(jsonids), hotel_id: hotel_id}, success : function(response) { $('#be-images ul').prepend(response).fadeIn(&apos ...

Ways to verify if an ajax function is currently occupied by a previous request

Is there a way to determine if an ajax function is occupied by a prior call? What measures can be taken to avoid invoking an ajax function while it is still processing a previous request with a readyState != 4 status? ...

Why does the lazy loading feature keep moving the background image around?

While trying to incorporate lazy loading on my website, I encountered an issue where the image position would change after it was fully loaded and made visible. I experimented with rearranging the order in which my JavaScript and CSS files were called, bu ...

Unexpected values in AJAX <select> dropdowns occur sporadically

I'm encountering an issue with a section of Ajax code (I am not very familiar with ajax) that was written by someone else. The Ajax code has some parts that work and others that do not: <?php include('includes/db_connection.php'); inclu ...

Inspecting Ajax response for specific CSS class - is it possible?

I am utilizing Ajax to send a request to a .Net MVC controller, which then returns HTML content to be displayed on a specific section of the webpage. My goal is to identify if this HTML contains a class name so that I can use it to update another section o ...

Updating the image sources of a group of image tags with a predetermined list

Looking to update a series of image source references within a specific div tag. For example: <!-- language-all: lang-html --> <div id="Listofimages"> <img src="images\2page_img_3.jpg"> <img src="images\2page_img_3 ...

Guide to setting up an automated process in PHP

When setting up a tournament interface on my page: Users utilize functions like fopen() and fwrite() to create tournaments. The created tournaments must only start after a specific time, for example, 1 hour. This delay allows other users to join the tour ...

Encountering an error in Asp.net MVC when attempting to open a modal window for the second time

My goal is to integrate a bootstrap modal window when displaying details. The modal window should open on an ajax call, but I'm encountering an issue where it only opens once. Initially, it opens with the entire template which is not desired. On subse ...

After changing the page, the Facebook JS SDK fails to function properly when using JQueryMobile

I'm facing an issue with my webapp that utilizes jQuery Mobile for full ajax navigation. I have initialized the Facebook SDK at the pageinit event in jQueryMobile (called on each page). jQuery(document).on('pageinit', function (event) { ...

Error: The first certificate could not be verified, although I included rejectUnauthorized: false option

I have encountered an issue with my getServerSideProps() function, as it is throwing an error when trying to call an external API: FetchError: request to https://nginx/api/items failed, reason: unable to verify the first certificate The self-signed cert ...

Can JavaScript be used to update/override Prototype version 1.4 to version 1.7 on a different website?

(I'm uncertain about the best way to phrase this question, feel free to make changes). I am in the process of embedding a JS widget onto a different website that is using Prototype.js Version 1.4. I have incorporated jQuery into my widget and have it ...

Assistance needed with updating a related element using jQuery AJAX upon successfully completing a

Currently, I am in the process of creating a database-driven jokes website where users have the ability to vote on their favorite jokes. Each time a user votes for a joke, the "score" column in the MySQL database is incremented. Now, my main concern is upd ...

The CKEditor value is set to the result of the dropdown selection

I need to implement a dropdown feature on my form where the options correspond to titles of content in my database. Once an option is selected, I want the corresponding content to display in a CKEditor field. I'm attempting to achieve something simil ...

Managing and comparing category IDs in JavaScript to effectively store and render subcategories

My goal is to set the current category ID in a variable, and if the category changes, I want to store that as well. Then, I need to compare both IDs. If they are not equal, I want to set the subcategory to null. However, I am unsure of where my mistake lie ...

When jQuery is used to move rows between tables, it can interfere with

When moving rows between tables, everything seems to work fine. However, once the row is moved to the other table, all JavaScript functions related to that row stop working, and the reason remains unknown. The JavaScript code is simple - it involves takin ...