0

I am trying to post some data to a repo on GitLab using the JavaScript Fetch API and it's not working. Here is my JavaScript code:

const url = "https://gitlab.com/api/v4/projects/XXXXXXXX/repository/files/first-post%2Emd";

const request = new Request( url );

fetch( request, {
    method: 'POST',
    headers: { "private-token": "xxxxxxxxxxxxxxxxxxxx", '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"}

} ).then( ( response ) => {

    if ( response.ok ) {

        return response.json();

    } else {

        return Promise.reject( response );
    }

} ).then( ( data ) => {

    document.write (JSON.stringify( data ) );

} ).catch( ( err ) => {

    console.warn( 'Something went wrong.', err );

} );

What is wrong here?

The response I get in the console:

PUT https://gitlab.com/api/v4/projects/13175347/repository/files/first-post.md?ref=master 400 (Bad Request) fetch-gitlab-post-0-00.html:34 Something went wrong. Response {type: "cors", url: "https://gitlab.com/api/v4/projects/13175347/repository/files/first-post.md?ref=master", redirected: false, status: 400, ok: false, …} (anonymous) @ fetch-gitlab-post-0-00.html:34 Promise.catch (async) (anonymous) @ fetch-gitlab-post-0-00.html:32

Here is the curl command that is working that I am trying to follow:

curl --request PUT \
--header "PRIVATE-TOKEN: xxxxxxxxxxxxxxxxxxxx" \
--header "Content-Type: application/json" \
--data '{"branch": "master", "author_email": "author@example.com", 
"author_name": "Firstname Lastname", "content": "some content \nabc 123\ntra 
la\nhello world", "commit_message": "create a new file"}' \
https://gitlab.com/api/v4/projects/XXXXXXX/repository/files/project2%2Emd
Theo
  • 376
  • 1
  • 3
  • 13
  • 1
    most probably due to 2 headers `header: { "private-token": "xxxxxxxxxxxxxxxxxxxx", 'Content-Type': 'application/json' }` – joyBlanks Sep 22 '19 at 04:34
  • source edited to a single headers item, but the same errors occur – Theo Sep 24 '19 at 00:38
  • 2
    `data` is not part of the [`Request` API](https://developer.mozilla.org/en-US/docs/Web/API/Request). You probably meant `body` and you probably also meant to run it through `JSON.stringify()` – Phil Sep 24 '19 at 00:39
  • 1
    Please see the linked duplicate. You want `fetch(url, { method: 'PUT', headers: { 'private-token': 'xxx', 'content-type': 'application/json' }, body: JSON.stringify({ branch: 'master', ... }) })` – Phil Sep 24 '19 at 02:23
  • @Phil Thanks. I tried changing 'data' to 'body. And I am looking at https://developer.mozilla.org/en-US/docs/Web/API/Request but I am not there yet. I did add a working curl command to the question. the curl is from: https://docs.gitlab.com/ee/api/repository_files.html#create-new-file-in-repository. – Theo Sep 24 '19 at 02:23
  • @Phil Yay! Ponies and kittens and all good things to you. Thank you for answering the question. – Theo Sep 24 '19 at 02:31

0 Answers0