Repository Files API start_branch doesn't work as expected

Summary

Following the examples from https://docs.gitlab.com/ee/api/repository_files.html#create-new-file-in-repository to create a new file via repository files API the start_branch parameter doesn't seem to work properly.

It always results in a 400 {message: You can only create or edit files when you are on a branch} error

Steps to reproduce

  1. Create a new repository and initialise it with a README (you'll have a default main branch).
  2. Try the following API calls to commit a new file to a new branch using start_branch:

(a): leaving branch empty:

curl --silent --fail --show-error --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN_2" \                       ✘ 22 feature/commit ✱ ◼
     --header "Content-Type: application/json" \
     --data '{"branch": "", "start_branch": "foobar", "author_email": "author@example.com", "author_name": "Firstname Lastname",
               "encoding": "base64", "content": "bWVvdyBtZW93IG1lb3c=", "commit_message": "create a new file"}' \
     "http://192.168.1.143:8080/api/v4/projects/55/repository/files/meow.txt"  -v
*   Trying 192.168.1.143...
* TCP_NODELAY set
* Connected to 192.168.1.143 (192.168.1.143) port 8080 (#0)
> POST /api/v4/projects/55/repository/files/meow.txt HTTP/1.1
> Host: 192.168.1.143:8080
> User-Agent: curl/7.64.1
> Accept: */*
> PRIVATE-TOKEN: 1zgm2s646XCSTy_8fzd3
> Content-Type: application/json
> Content-Length: 226
>
* upload completely sent off: 226 out of 226 bytes
* The requested URL returned error: 400 Bad Request
* Closing connection 0
curl: (22) The requested URL returned error: 400 Bad Request

(b) setting branch to same value as start_branch:

curl --silent --fail --show-error --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN_2" \                       ✘ 22 feature/commit ✱ ◼
     --header "Content-Type: application/json" \
     --data '{"branch": "foobar", "start_branch": "foobar", "author_email": "author@example.com", "author_name": "Firstname Lastname",
               "encoding": "base64", "content": "bWVvdyBtZW93IG1lb3c=", "commit_message": "create a new file"}' \
     "http://192.168.1.143:8080/api/v4/projects/55/repository/files/meow.txt"  -v
*   Trying 192.168.1.143...
* TCP_NODELAY set
* Connected to 192.168.1.143 (192.168.1.143) port 8080 (#0)
> POST /api/v4/projects/55/repository/files/meow.txt HTTP/1.1
> Host: 192.168.1.143:8080
> User-Agent: curl/7.64.1
> Accept: */*
> PRIVATE-TOKEN: 1zgm2s646XCSTy_8fzd3
> Content-Type: application/json
> Content-Length: 232
>
* upload completely sent off: 232 out of 232 bytes
* The requested URL returned error: 400 Bad Request
* Closing connection 0
curl: (22) The requested URL returned error: 400 Bad Request

(c): setting branch to main (which exists):

curl --silent --fail --show-error --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN_2" \                       ✘ 22 feature/commit ✱ ◼
     --header "Content-Type: application/json" \
     --data '{"branch": "main", "start_branch": "foobar", "author_email": "author@example.com", "author_name": "Firstname Lastname",
               "encoding": "base64", "content": "bWVvdyBtZW93IG1lb3c=", "commit_message": "create a new file"}' \
     "http://192.168.1.143:8080/api/v4/projects/55/repository/files/meow.txt"  -v
*   Trying 192.168.1.143...
* TCP_NODELAY set
* Connected to 192.168.1.143 (192.168.1.143) port 8080 (#0)
> POST /api/v4/projects/55/repository/files/meow.txt HTTP/1.1
> Host: 192.168.1.143:8080
> User-Agent: curl/7.64.1
> Accept: */*
> PRIVATE-TOKEN: 1zgm2s646XCSTy_8fzd3
> Content-Type: application/json
> Content-Length: 230
>
* upload completely sent off: 230 out of 230 bytes
* The requested URL returned error: 400 Bad Request
* Closing connection 0
curl: (22) The requested URL returned error: 400 Bad Request

What is the current bug behavior?

400 error

What is the expected correct behavior?

creates the file on a new branch

Edited by See GitLab account @timofurrer