0

I am trying to use a file, after it has been uploaded with the and then create an AJAX request with the file and send it to the einstein vision (predict) api endpoint to get back results of what the photo is. I was given the below curl command through the API documentation and am having a little bit of trouble converting correctly.

curl command:

curl -X POST -H "Authorization: Bearer <Token>" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "sampleContent=@generalimage.jpg" -F "modelId=GeneralImageClassifier" https://api.einstein.ai/v2/vision/predict

ajax code: (I need to add form data of model id and samplecontent of the image)

var uploadBtn = $('#upload-button');
uploadBtn.change(function(event) {
    event.preventDefault();
    var file = event.target.files[0].name;
    var formData = new FormData();
    formData['sampleContent'] = file;
    formData['modelId'] = 'GeneralImageClassifier';

    $.ajax({
        url: 'https://api.einstein.ai/v2/vision/predict',
        method: 'POST',
        processData: false,
        contentType: false,
        data: ???,
        headers: {
            'Authorization': 'Bearer <Token>',
            'Cache-Control': 'no-cache',
            'Content-Type': 'multipart/form-data'
        }
    });
});
Grim
  • 1,938
  • 10
  • 56
  • 123
cal
  • 47
  • 1
  • 9
  • 1
    You can't upload a filename from Javascript, the file has to be selected by the user from an `` – Barmar Oct 20 '17 at 01:02
  • I have that - but after the file loads from the input, how do I send the photo to an api endpoint? I am having a hard time with the syntax. – cal Oct 20 '17 at 21:11
  • Isn't that explained in the linked question? – Barmar Oct 20 '17 at 21:18
  • Everything I have tried from the linked post, I still get "400 Bad Request" – cal Oct 20 '17 at 21:22
  • It should be `event.target.files[0]`, don't put `.name`. – Barmar Oct 20 '17 at 21:25
  • I only tried that because the curl command only sends the sampleContent=somefile.jpg - even removing the name, it still returns Bad Request – cal Oct 20 '17 at 21:28
  • It looks like you can't use `headers:` to set the `Authorization:` header. See https://stackoverflow.com/questions/7433556/jquery-jsonp-ajax-authentication-header-not-being-set – Barmar Oct 20 '17 at 21:34

0 Answers0