【问题标题】:Uploading file with Octoprint API and GuzzleHttp使用 Octoprint API 和 GuzzleHttp 上传文件
【发布时间】:2017-07-27 08:31:45
【问题描述】:

我正在尝试通过其 API 将 gcode 文件上传到我的 octoprint。 在此处链接到其文档:http://docs.octoprint.org/en/master/api/files.html#upload-file-or-create-folder

Octoprint 响应内部服务器错误。 在检查其日志文件后,我发现以下几行表明 octoprints 上传功能存在问题

 File "/home/pi/oprint/local/lib/python2.7/site-packages/OctoPrint-1.3.4-py2.7.egg/octoprin$
added_file = fileManager.add_file(FileDestinations.LOCAL, futureFullPathInStorage, uploa$
File "/home/pi/oprint/local/lib/python2.7/site-packages/OctoPrint-1.3.4-py2.7.egg/octoprin$
self._analysis_queue.dequeue(queue_entry)
File "/home/pi/oprint/local/lib/python2.7/site-packages/OctoPrint-1.3.4-py2.7.egg/octoprin$
if not entry.type in self._queues:
AttributeError: 'NoneType' object has no attribute 'type'

我正在使用GuzzleHttp上传文件,调用api url /api/files/local

public function printFile(Raspi $raspi,$file) {
    $fileName = basename($file); 

    $result = $this->callAPIMethod($raspi, '/api/files/local', [
        'multipart' => [
            [
                'name'     => 'file',
                'contents' => Storage::get($filePath),
                'filename' => $fileName
            ],
            [
                'name'     => 'print',
                'contents' => true,
            ]
        ]  
    ],"post");
    $raspi->status = "druckt";
    $raspi->save();
}

private function callAPIMethod(Raspi $raspi,$apiPath,$commandParams,$method='get') {
    $client = new Client(); 
    $apiKey = $raspi->key;
    $params = [ 
        'headers' => [
            'X-API-Key' => $apiKey,
        ]
    ];
    if($commandParams != NULL)
        $params = array_merge ($params, $commandParams);
    switch($method) {
        case "get":    
            return $client->get($raspi->ip.":".RaspiApiController::$APIPORT."/".$apiPath, $params);
        break;
        case "post":    
                return $client->post($raspi->ip.":".RaspiApiController::$APIPORT.$apiPath, $params);
        break;
        case "delete":    
            return $client->delete($raspi->ip.":".RaspiApiController::$APIPORT."/".$apiPath, $params);
        break;
        default:
            return $client->get($raspi->ip.":".RaspiApiController::$APIPORT."/".$apiPath, $params);
        break;
    }
}

如何在 Guzzle 中更改我的请求以防止发生此错误?

【问题讨论】:

    标签: php guzzle octoprint


    【解决方案1】:

    问题原来是上传的文件没有正确的文件扩展名,而 octoprint api 只是在发送带有错误扩展名的文件时崩溃。

    据我的测试显示,octoprint api 只能正确上传 .gcode 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2014-03-13
      • 1970-01-01
      • 2022-09-30
      相关资源
      最近更新 更多