SAP Leonardo Image Feature Extraction API(https://sandbox.api.sap.com/ml/featureextraction/inference_sync)をテストしています。私は画像のbase64文字列を持っていて、ファイルオブジェクトに変換して圧縮し、XMLHttpRequestを使用してこのAPIに圧縮画像ファイルを送信します。しかし、応答テキストは「サービスは(圧縮された)画像のリストを必要とする」である。FormDataを使用してSAP Leonardo Image Feature Extraction APIにファイルが送信されていません
HTTPリクエストヘッダとパラメータを以下のスクリーンショットに添付します。
我々はパラメータで汚いコードを参照してくださいますが、ZIPファイルDownload hereが正常に作成されます。
あなたは、ZIPファイルをダウンロードできない場合は、下のスクリーンショットを参照してください。
すべてが正常であると思われます。しかし、応答テキストは、私のjavascriptのコードは以下のようになり、ステータス400
と以下の通りです。なにが問題ですか?それは私が自分でこの問題を解決
dataURItoBlob: function(dataURI, fileName) {
\t //convert base64/URLEncoded data component to raw binary data held in a string
\t var byteString;
\t if (dataURI.split(',')[0].indexOf('base64') >= 0)
\t \t byteString = atob(dataURI.split(',')[1]);
\t else
\t \t byteString = unescape(dataURI.split(',')[1]);
\t
\t //separate out the mime component
\t var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
\t
\t //write the bytes of the string to a typed array
\t var ia = new Uint8Array(byteString.length);
\t for (var i = 0; i < byteString.length; i++) {
\t \t ia[i] = byteString.charCodeAt(i);
\t }
\t
\t var blob = new Blob([ia], {encoding:"UTF-8",type:mimeString});
\t //A Blob() is almost a File() - it's just missing the two properties below which we will add
\t blob.lastModifiedDate = new Date();
\t blob.name = fileName + '.' + mimeString.split('/')[1];
\t
\t return blob;
},
onSubmit: function(oEvent) {
\t var oImage = this.getView().byId('myImage');
\t //oImage.getSrc() : 'data:image/png;base64,iVBORw0KGgo...'
\t var imageFile = this.dataURItoBlob(oImage.getSrc(), 'myImage');
\t var zip = new JSZip();
\t zip.file(imageFile.name, imageFile);
\t
\t zip.generateAsync({
\t \t type:"blob",
\t \t compression: 'DEFLATE', // force a compression for this file
\t \t compressionOptions: {
\t \t \t level: 6,
\t },
\t }).then(function(content) {
\t \t //saveAs(content, "hello.zip");
\t \t // start the busy indicator
\t \t var oBusyIndicator = new sap.m.BusyDialog();
\t \t oBusyIndicator.open();
\t \t
\t \t var formData = new FormData();
\t \t formData.append('files', content, 'myImage.zip');
\t \t
\t \t var xhr = new XMLHttpRequest();
\t \t xhr.withCredentials = false;
\t \t
\t \t xhr.addEventListener("readystatechange", function() {
\t \t \t if (this.readyState === this.DONE) {
\t \t \t \t oBusyIndicator.close();
\t \t \t \t //navigator.notification.alert(this.responseText);
\t \t \t \t console.log(this.responseText);
\t \t \t }
\t \t });
\t \t //setting request method
\t \t //API endpoint for API sandbox
\t \t //Destionation '/SANDBOX_API' in HCP is configured as 'https://sandbox.api.sap.com'
\t \t var api = "/SANDBOX_API/ml/featureextraction/inference_sync";
\t \t xhr.open("POST", api);
\t \t //adding request headers
\t \t xhr.setRequestHeader("Content-Type", "multipart/form-data");
\t \t xhr.setRequestHeader("Accept", "application/json");
\t \t //API Key for API Sandbox
\t \t xhr.setRequestHeader("APIKey", "yQd5Oy785NkAIob6g1eNwctBg4m1LGQS");
\t \t //sending request
\t \t xhr.send(formData);
\t });
\t
},
それはあなたのことを、あなたはそれを公開することにしたい確信している、あなたのAPIキーを配るてうれしいですか!パブリックにしない場合は、apiキーを無効にして、新しいものをリクエストしてください。 – Icepickle
これを思い出してくれてありがとう!私はAPIKeyのいくつかの文字を '...'で修正します。 –
それは本当に問題ではなく、編集履歴からすべてを読むことができます。質問を削除して新しいものを書くとしても、まだ漏れていますあなたのapiキーをネット上に出してください。キーを無効にして、ビジネス上の重要な問題になる可能性がある場合は、新しいものを手に入れようとします。 – Icepickle