powershell: bitbucket api post request returns 400 error - rest

I'm using powershell to work with Bitbucket API. Powershell sends command to check, if pull request could be merged and then merges it.
First command works without any problems:
Invoke-WebRequest -Headers #{Authorization = "Basic $base64AuthInfo"} -Method Get -ContentType "application/json" -Uri "$BaseUrl/rest/api/1.0/projects/$ProjectKey/repos/$RepoSlug/pull-requests/$PullRequestId/merge?version=$PRVersion"
I'm getting a valid JSON response. Example:
{"canMerge":true,"conflicted":false,"outcome":"CLEAN","vetoes":[]}
The second command must use POST request in order to merge the pull request. Same command with Post instead of Get :
Invoke-WebRequest -Headers #{Authorization = "Basic $base64AuthInfo"} -Method Post -ContentType "application/json" -Uri "$BaseUrl/rest/api/1.0/projects/$ProjectKey/repos/$RepoSlug/pull-requests/$PullRequestId/merge?version=$PRVersion" -Verbose
This one returns 400 error without any other information.
VERBOSE: POST https://bitbucket.example.com/rest/api/1.0/projects/TEAM/repos/tmp-test-repo/pull-requests/8/merge?version=2 with 0-byte payload
Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At line:1 char:1
+ Invoke-WebRequest -Headers #{Authorization = "Basic $base64AuthInfo"} ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
How can make POST request work?
Reference: Bitbucket documentation

The problem is that Powershell doesn't output expanded details in Invoke-WebRequest output, so this makes investigation really hard.
Turned out that the problem was in account I was using. After I used Fiddler to get the response headers, I got a detailed response from Bitbucket API, so I was able to fix my problem.

Related

Connect to WebHDFS using powershell: How to set the Different Credentials

I am rtying to connect te WebHDFS by powershell and have been retrieving some errors. I think the 401 error is because of the Credentials.
The code I've been using is:
Invoke-RestMethod -UseDefaultCredentials -Method Put -inFile c:\Users\petter\Documents\example.txt -Uri "https://drona-haproxy.corp.com:2000/gateway/drona/webhdfs/v1/user/petter/example.txt?op=CREATE"
How could I set different parameters in the Credentials under "Invoke-RestMethod"?
THe error that I've been retrieving is:
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At line:1 char:1
+ Invoke-RestMethod -Method Put -inFile c:\Users\petter\Documents\load ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
You can use the Credential parameter with Invoke-RestMethod.
I would have thought you'd need to remove -UseDefaultCredentials but the Invoke-RestMethod documentation says they're part of the same parameter set - not sure how that works.
related: WebHDFS Authentication

Rest API call works with curl but doesn't work with PowerShell

I have following curl request that works
curl.exe -X GET "https://host.crm.dynamics.com/api/data/v9.1/accounts?$select=name" -H "Authorization: Bearer xxxxx"
And want to implement it in PowerShell
Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
-Headers #{Authorization = "Bearer xxxxx"} `
-Method Get
I've also tried to do request with Invoke-WebRequest as well as curl version in Powershell and it didn't work
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At E:\Repos\myrepo\host\connection_test.ps1:51 char:1
+ Invoke-RestMethod $hostName `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
When making connection using Invoke-RestMethod, you want to make sure that encryption your client is using is accepted by the URI.
PowerShell by default uses TLS 1.0 for web requests but the URI you are connecting to probably uses 1.1 or 1.2. You can use the following to force the use of TLS v1.1 or v1.2.
[Net.ServicePointManager]::SecurityProtocol = `
[Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12;
Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
-Headers #{Authorization = "Bearer xxxxx"} `
-Method Get

Having trouble uploading large files with Invoke-RestMethod

I am trying to use the Hashicorp Atlas restful api to upload box files into but I am running into problems when uploading large files.
I am currently using the following command which works with small files like 100mb but most box files are well over 4GB where I start to see the problem:
$Filename = "c:\box\mybox.box"
$uploadpath = "https://archivist.hashicorp.com/v1/object/example"
$Timeout = 86400 #24 hours
Invoke-RestMethod -Uri $uploadPath -Method Put -InFile $Filename -TimeoutSec $Timeout -ContentType "multipart/form-data"
The error:
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At C:\Program Files\WindowsPowerShell\Modules\atlasbox\1.1.15\AtlasBox.psm1:642 char:5
+ Invoke-RestMethod -Uri $uploadPath -Method Put -InFile $Filename ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
If I use CURL command as per the documentation it works fine.
curl -X PUT --upload-file /path/to/my.box https://archivist.hashicorp.com/v1/object/example
Any idea how to make Invoke-RestMethod behave the same way curl does?
I can't take a dependency on curl because not all systems I run this on will have WSL installed and no curl.
Documentation for Atlas API is here: https://atlas.hashicorp.com/help/api/vagrant/box-providers
Looks like it might be getting weird on the SSL cert? I would try some of the recommendations here to start with, definitely an oddity specific to those cmdlets though:
Powershell v3 Invoke-WebRequest HTTPS error

Recreating Curl -F with Invoke-WebRequest -InFile

I am trying to post a file to an api using powershell. I am able to post to the api with curl -F but not with Invoke-WebRequest ...-infile . I get an internal server error. I am assuming that it has something to do with how the file is broken up but I am not sure.
Invoke-RestMethod -Uri 'myapi' -ContentType 'multipart/form-data' -Method Post -Headers $headers -Body $Fields
Where:
$headers = #{'Authorization' : 'Basic <Token>'}
Error:
Invoke-RestMethod :
500 Internal Server Error
Internal Server Error
The server encountered an internal error or
misconfiguration and was unable to complete
your request.

PowerShell Invoke-RestMethod cmdlet failing to send second authentication challenge?

When using PowerShell's Invoke-RestMethod to a service that uses Windows Authentication, I'm getting a "401.2 Not Authorized" Exception, but I've confirmed via
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
that I'm using the expected user. I've verified that user is allowed in my service's web.config. Here's my actual invocation:
Invoke-RestService -Method "POST" -Uri $serviceApiUri -Body (ConvertTo-Json $body) -UseDefaultCredentials -ContentType "application/json"
Anyone else have issues with PowerShell's Invoke-RestMethod with Windows Authentication? How can I fix this?