Search code examples
jqueryazureelectronface-recognitionazure-cognitive-services

Microsoft Face API - 400 Request Body is invalid


I am using the Microsoft Face API to build a Facial recognition desktop app using Electron. I can right now detect a face and create a person group, but run into this error when I try and add a person to my person group:

{"error":{"code":"BadArgument","message":"Request body is invalid."}}, 

which is marked as Error 400. Bad request on my console.

This is the API page on how to use this request:

Here is my code, obviously something is wrong with the Data field, but when I use the exact same data in the westCentralUS test server, it is successful. I have tried using and omitting the optional userData field, with a string and an image file.

function createPerson() {

var params = {
        // Request parameters
    };

    $.ajax({
        url: "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/persongroups/students/persons",
        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Content-Type","application/json");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key",apiKey);
        },
        type: "POST",
        // Request body
        data: { name: "John",}
    })
    .done(function(data) {
        alert("success");
    })
    .fail(function() {
        alert("error");
    });
}

Solution

  • Try

    data: JSON.stringify({name: "John"})
    

    instead.