File upload using Gitlab v4 API

Hi,

I have been using the Gitlab API v4 for integration. I have a new requirement to upload files I have followed this Upload File Docs

When I use the cURL, it is working fine the file is uploaded correctly but when I convert it to guzzle and then try I get an error saying the file is invalid.

Here is my cURL

curl --request POST --header "PRIVATE-TOKEN: {api-token}" https://gitlab.com/api/v4/projects/usamarehank%2Flabel-functionality/uploads --form "file=@../../../public/storage/defect/2q4.jpg"

Here is my guzzle

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => $currBase[1].'/',
    // You can set any number of default request options.
    // 'timeout'  => 2.0,
]);

try {
    $projectNamespace = self::convertToURLEncoded($gitlabData[0]->channel);

    $response = $client->request('POST', '/api/v4/projects/'.$projectNamespace.'/uploads',[
            'headers' => [
                'Private-Token'     => $gitlabData[1]->apikey,
                'Content-Type' =>  'multipart/form-data'
            ],
            'form_params' => [
                'file' => ' @../../../public/storage/defect/2q4.jpg'
            ]
        ]
    );
}
    // catch for bad credentials exception
catch (GuzzleHttp\Exception\BadResponseException $e){
    return ['status' => false, 'msg' => 'Incorrect Credentials'];
}

// if 200 then successful
if($response->getStatusCode() === 201 || $response->getStatusCode() === 200){
    $responseContent = json_decode($response->getBody(), true);
    self::insertGeneratedIssueDetails($responseContent, $_projectID, $_defectID);
    return ['status' => true, 'msg' => 'Authentication Successful'];
}

Any helo with be highly appreciated.

1 Like