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.
Related
I'm trying to download a file with the Azure Devops Rest API using Powershell's Invoke-RestMethod. It always seems to download from the main branch and ignores my branch specification
The urls I'm using are
invoke-restmethod -uri "https://dev.azure.com/company/xxx/_apis/git/repositories/xxx/items?path=%2Fpom.xml&commitOrBranch=main&api-version=6.0" -Method Get -ContentType "application/text" -Headers $headers
and
invoke-restmethod -uri "https://dev.azure.com/company/xxx/_apis/git/repositories/xxx/items?path=%2Fpom.xml&commitOrBranch=branchName&api-version=6.0" -Method Get -ContentType "application/text" -Headers $headers
How do I specify the branch? The documentation doesn't provide any details
You could try the below commandlet.
Just modified the below based on the question to download a pom.xml from the <BranchName>
invoke-restmethod -uri "https://dev.azure.com/company/xxx/_apis/git/repositories/xxx/items/pom.xml?versionType=Branch&version=<BranchName>" -Method Get -ContentType "application/text" -Headers $headers
I want to run curl command in PowerShell but I am facing error that I am not able to connect SSL/TLS
$loginurl= https://education.org/logon
$data= New-Object "System.collections.Generic.Dictionary[[String],[String]]"
$data.Add('username','abc')
$data.Add('password','abc')
$method=POST
$response= Invoke-RestMethod -Method $method -Uri $loginurl -Body $data
$response.RawContent
I tried writing in Shell it worked but in shell I used --insecure-sS as another switch to work. Can anyone please help how to resolve this in PowerShell and sometimes it gives me syntax error as well.
I think you need the -SkipCertificateCheck switch:
Invoke-RestMethod -Method $method -Uri $loginurl -Body $data -SkipCertificateCheck
Incidentally, a simpler way to create your hashtable is like this:
$data = #{'username'='abc';'password'='abc'}
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.
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?
I am using invoke-webrequest command to load a file on server with POST command, and the file does get loaded. However the command hangs in console until I hit enter.
Here's what I m executing:
Invoke-RestMethod -Uri $url -Method POST -ContentType $ContentType -Headers $headerHash -InFile $filePath -TimeoutSec $timeOut