0

I'm using the Alfresco Rest API from a Laravel application!

To do so, I use the laravel guzzlehttp/guzzle package.

Below is my code.

When I run it, I get a status 400

The documentation of my endpoint can be found here: 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

    }

}

Homerite
  • 1
  • 2

2 Answers2

1

I dont understand why you have used $querytype but as you have asked in your heading "How to upload file using Laravel Guzzle HTTP client", so here is the answer for that,

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;
    }

You can see withToken() method in docs

bhucho
  • 3,903
  • 3
  • 16
  • 34
0

The response should mention what precipitated the bad request. You may try wireshark to capture the upload attempt and compare it with the curl examples here

Curtis
  • 548
  • 3
  • 13