code_description错误的请求代码400,错误流为xx个字节,但至少必须为100个字节

时间:2018-10-26 13:57:41

标签: laravel guzzle ibm-watson speech-to-text bad-request

我正在尝试在laravel应用程序中使用ibm语音文本api。提交音频文件后,我继续收到此错误:

{"request error":"POST \/speech-to-text\/api\/v1\/recognize HTTP\   \"code_description\": \"Bad Request\", \n   \"code\": 400, \n   \"error\": 
    \"Stream was 25 bytes but needs to be at least 100 bytes.\"\n}"}`

这是我的代码:

    <?php


namespace App;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7;
use Mockery\Exception;
use Request;
use RobbieP\CloudConvertLaravel\Facades\CloudConvert as CloudConvert;
use Storage;

class IbmWatson
{

    public function getPersonalityTraits($request)
    {
        $client = new Client();
        $options = [
            'auth' => [
                env('PERSONALITY_INSIGHTS_USERNAME'),
                env('PERSONALITY_INSIGHTS_PASSWORD')
            ],
            'headers' => [
                'content-type' => 'text/plain',
            ],
            'body' => $request

        ];
        $r = $client->request("POST", env('PERSONALITY_INSIGHTS_BASEURL'), $options);
        return json_decode($r->getBody(), true);
    }
    //
      private function convertFileToMp3($request)
     {
         $path = '../storage/app/mp3/';
         Storage::disk('local')->put('mp3', $request); //the storage/app/mp3 folder must be created using the put method
          //CloudConvert::file($request)->to($path . $request->getClientOriginalName() . '.mp3'); //converting... saving in /storage/app/mp3
      }

    public function getSpeechToText($request)
    {
      //dd($request);
         // try {
         //     $this->convertFileToMp3($request);
         // } catch (\Exception $e) {
         //     return 'Something wrong happened.';
         // }

         //dd(env('SPEECH_TEXT_USERNAME'));

        // \Auth::user()->videos()->create([
        //     'path' => '/mp3/',
        //     'name' => $request->getClientOriginalName()
        // ]);

        $client = new Client();
        $options = [
            'auth' => [
                env('SPEECH_TEXT_USERNAME'),
                env('SPEECH_TEXT_PASSWORD')
            ],
            'headers' => [
                'content-type' => 'audio/mp3', //accepts audio/mp3; audio/webm; audio/flaac; audio/opus; audio/wav;
            ],
            'body' => $request //Storage::get(Video::orderBy('id', 'desc')->first()->path . Video::orderBy('id', 'desc')->first()->name) //using the storage/app/mp3/file.mp3
        ];
        $r = $client->request("POST", 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize', $options);
        return json_decode($r->getBody(), true);
    }
}

我尝试将带有表单数据(Laravel应用程序)的POST请求发送到ibm语音到文本api。

但是我收到(400 Bad Requestresponse :)错误,code_description:错误请求,n个代码400,n个错误流是25个字节,但至少需要100个字节。

我正在使用guzzle发送发帖请求。我不知道代码有什么问题,其他文件都正常工作。

我已经遍历了ibm的演讲,获得了文本文档,但是我没有找到任何帮助。任何对这个问题有想法的人都可以帮助!!预先谢谢你

0 个答案:

没有答案