如何解决如何使用Laravel Guzzle HTTP客户端上传文件
我正在使用Laravel应用程序中的Alfresco Rest API!
为此,我使用了laravel guzzlehttp / guzzle软件包。
下面是我的代码。
运行它时,我的状态为400
我的端点的文档可以在这里找到:https://api-explorer.alfresco.com/api-explorer/#!/nodes/createNode
// AlfrescoService.PHP
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Response;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
class AlfrescoService
{
public static function apiConnexion()
{
$response = Http::withHeaders([
"Content-Type" => "application/json",])->post('http://192.168.43.152:8080/alfresco/api/-default-/public/authentication/versions/1/tickets',[
'userId' => 'admin','password' => 'admin',]);
return base64_encode( $response["entry"]["id"] );
}
public static function request2($queryType,String $query,array $data=[])
{
$response = Http::withHeaders([
"Authorization" => "Basic ".self::apiConnexion(),])->attach(
'attachment',file_get_contents('alfresco/doc.txt'),'doc.txt'
)->$queryType('http://192.168.43.152:8080/alfresco/api/-default-/public/alfresco/versions/1'.$query,$data);
return $response;
}
}
// AlfrescoController.PHP
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\AlfrescoService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use MercurySeries\Flashy\Flashy;
class AlfrescoController extends Controller
{
public function storeFile(Request $request) {
$data=["name"=>"My new File.txt","nodeType"=>"cm:content"];
$response=AlfrescoService::request2("post","/nodes/-shared-/children",$data);
dd($response->status()); // 400
}
}
解决方法
响应应提及导致不良请求的原因。您可以尝试wireshark捕获上载尝试,并将其与curl示例here
进行比较 ,我不明白您为什么使用$ querytype,但是正如您在标题“如何使用Laravel Guzzle HTTP客户端上载文件”中所询问的那样,这就是答案,
public static function request2($queryType,String $query,array $data=[])
{
$file = fopen('alfresco/doc.txt','r')
$response = Http::withToken(self::apiConnexion())
->attach('attachment',$file)
->post($url);
return $response;
}
您可以在docs中看到withToken()方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。