2
votes

I'm working with the REST API for Gitlab, part currently we need to update one submodule using the API, in curl the process should be something like this:

curl --request PUT -v --header "PRIVATE-TOKEN: XXXXXXXXXX" "http://mygitlab.com/api/v4/projects/1/repository/submodules/MYSUBMODULE" \
  --data "branch=master&commit_sha=6bf09c4a3dc4ca79cbe301e6add06005f38ad9e3&commit_message=Update submodule reference"

And with curl this work without any problem, but we need to do this using PowerShell, we are trying to use Invoke-RestMethod for Powershell 5.1 and according to the documentaion should be something like this:

Invoke-RestMethod -Headers @{"PRIVATE-TOKEN"="$TOKEN"} -Method Put -Uri "http://mygitlab.com/api/v4/projects/1/repository/submodules/MYSUBMODULE" -Body "branch=master&commit_sha=6bf09c4a3dc4ca79cbe301e6add06005f38ad9e3&commit_message=Update submodule reference"

but our result is the following:

Invoke-RestMethod : {"error":"commit_sha is missing, branch is missing"}
At C:\version-manager\release.ps1:67 char:1
+ Invoke-RestMethod -Headers @{"PRIVATE-TOKEN"="$TOKEN"} -Method Put -U ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Please notice is like doesn't get the body, and as far as I know what in curl is --data in PowerShell should be -Body.

Any Idea how to fix this?

1
Try with -Body @{branch = 'master';commit_sha = '6bf09c4a3dc4ca79cbe301e6add06005f38ad9e3';commit_message = 'Update submodule reference'}Mathias R. Jessen
Same resoult :(aasanchez

1 Answers

0
votes

After some research, I found even in the documentation said:

-ContentType

Specifies the content type of the web request.

If this parameter is omitted and the request method is POST, Invoke-RestMethod sets > the content type to "application/x-www-form-urlencoded". Otherwise, the content type > is not specified in the call.

just after specifying the content type in the call as "application/x-www-form-urlencoded", which supposed to be the default value if is not specified, the call works.

The end solution was:

Invoke-RestMethod -Headers @{"PRIVATE-TOKEN"="$TOKEN"} -Method Put -Uri "http://mygitlab.com/api/v4/projects/1/repository/submodules/MYSUBMODULE" -Body "branch=master&commit_sha=6bf09c4a3dc4ca79cbe301e6add06005f38ad9e3&commit_message=Update submodule reference" -ContentType "application/x-www-form-urlencoded"

I hope this help to someone in the future.