0

I am working on GitLab API to commit an mp3 file and it was working for years without having problems and the last few weeks it is throwing errors

GuzzleHttp\Exception\ClientException: Client error: `POST https://gitlab.com/api/v4/projects/.../repository/commits/` resulted in a `400 Bad Request` response in file vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113

Here is the code I am using,

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

        $files = [
            [
                'name'     => 'branch',
                'contents' => 'TEST'
            ],
            [
                'name'     => 'commit_message',
                'contents' => 'Some message'
            ],
            [
                'name'     => 'actions[][action]',
                'contents' => 'update'
            ],
            [
                'name'     => 'actions[][file_path]',
                'contents' => 'gitlab filepath/test.mp3'
            ],
            [
                'name'     => 'actions[][content]',
                'contents' => file_get_contents('localfilepath/test.mp3')
            ],
        ];
        $response = $client->request('POST', '/api/v4/projects/.../repository/commits/', [
            'headers' => [
                'PRIVATE-TOKEN' => $TOKEN
            ],
            'multipart' => $files
        ]);

        return $response->getBody()->getContents();
    ```
    Thanks in Advance
Ann
  • 31
  • 1
  • 6
  • Did they change their API? – John Gordon Nov 13 '20 at 00:19
  • 1
    Sounds like a problem on the server to me. – miken32 Nov 13 '20 at 00:19
  • I am not sure gitlab changed their API .but am assuming that I was using some older version of GuzzleHttp and somehow it updated even though I dont remember I updated composer @miken32 Can you make it bit more clear about the possible server issue you have mentioned ? – Ann Nov 13 '20 at 16:27
  • The only changes that I am aware of is the change in public ip of my office internet. all our servers sits in google cloud. and I added public ip to firewall rules. do you see any dependency with this ? – Ann Nov 13 '20 at 17:46
  • If you haven't changed the code, and the server responds with an error, it seems reasonable to assume that something changed on the server. Either it's a temporary problem, or something you'll need to adjust your code to compensate for. – miken32 Nov 13 '20 at 19:50
  • I have the same 400 bad request error for all my unit test which has POST request too .and not sure where to change as everything was working fine and didn't do any changes in the code recently . I am using Laravel 7 . Only thing I changed in the server is added my new public ip to the firewall rules. – Ann Nov 13 '20 at 20:11
  • Server issues are fixed .Now other API calls are working fine .but still GitLab API is failed to upload mp3 files and throwing the same error "`400 Bad Request` If I change the value of file_get_contents('localfilepath/test.mp3') to base 64 ,I could see the commit is working but instead of mp3 file am seeing encoded text in the commit. Please help .I have no idea what is changed in GitLab API – Ann Nov 19 '20 at 20:24

1 Answers1

0

create-new-file-in-repository

POST /projects/:id/repository/files/:file_path
curl --request POST --header 'PRIVATE-TOKEN: <your_access_token>' \
     --header "Content-Type: application/json" \
     --data '{"branch": "master", "author_email": "author@example.com", "author_name": "Firstname Lastname", \
               "content": "some content", "commit_message": "create a new file"}' \
     "https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fproject%2Erb"

Parameters:

file_path (required) - URL encoded full path to new file. Ex. lib%2Fclass%2Erb
branch (required) - Name of the branch
start_branch (optional) - Name of the branch to start the new commit from
encoding (optional) - Change encoding to base64. Default is text.
author_email (optional) - Specify the commit author’s email address
author_name (optional) - Specify the commit author’s name
content (required) - File content
commit_message (required) - Commit message

Although the document shows that the param of "author_email" and "author_name " is optional, but without these two params will report 400 error, plus then works well

faith
  • 9
  • 2