Invoke-RestMethod or Invoke-WebRequest version of curl?

Invoke-RestMethod or Invoke-WebRequest version of curl?

Content Index :

Invoke-RestMethod or Invoke-WebRequest version of curl?
Tag : powershell , By : user104292
Date : November 28 2020, 11:01 PM

To fix this issue I'm trying to access a web api for Sonarqube on our local server. The examples in the Sonarqube documentation are all curl-based and I'm trying to translate them to PowerShell, either Invoke-WebRequest or Invoke-RestMethod. I'm having issues with credentials. I keep getting a 401 Not Authorized error. I've reviewed every post I can find on doing this and I don't see a comprehensive complete answer on how to do this. , I think you need to pass in a credential like this:
$username = "user"
$password = "password"
$authInfo = ("{0}:{1}" -f $username,$password)
$authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
$authInfo = [System.Convert]::ToBase64String($authInfo)
$headers = @{Authorization=("Basic {0}" -f $authInfo)}

Invoke-RestMethod -Method Head -Uri "https://pathtowhatever" -ContentType 'text/json' -Headers $headers

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to get HTTP status on Invoke-WebRequest/Invoke-RestMethod when using -OutFile parameter?

Tag : rest , By : Alecsandru Soare
Date : March 29 2020, 07:55 AM
may help you . As documented, use the -Passthru parameter if you want the response returned in addition to being written to a file.

Why is Invoke-WebRequest and Invoke-RestMethod failing and succeeding at the same time?

Tag : powershell , By : Milander
Date : March 29 2020, 07:55 AM
I wish this help you I went ahead and looked into the Invoke-WebRequest cmdlet code and found out why it's failing with this particular error.
It's failing on a call to System.Globalization.EncodingTable.GetCodePageFromName. The encoding is passed to that function as a parameter and the encoding is retrieved from the the cmdlet through the Content-Type header. In the case of this server the Content-Type was sent back in the response as Content-Type: application/xml; charset="UTF-8".
internal static Encoding GetEncodingOrDefault(string characterSet)
  string name = string.IsNullOrEmpty(characterSet) ? "ISO-8859-1" : characterSet;
    return Encoding.GetEncoding(name);
  catch (ArgumentException ex)
    return Encoding.GetEncoding((string) null);
if (name==null) { 
    throw new ArgumentNullException("name");

Invoke-WebRequest / Invoke-RestMethod failed with error underlying connection closed

Tag : rest , By : Heals1ic
Date : March 29 2020, 07:55 AM
I hope this helps you . You should use the -Header switch to pass your parameters. Though Invoke-WebRequest support header, I recommend using Invoke-RestMethod as it also return the Headers.
Try something like,
Invoke-RestMethod -Method Post -Uri http://localhost:3030/api/v1/usergroupsync -Body (ConvertTo-Json $body) -Header @{"apikey"=$apiKey}

Invoke-WebRequest\RestMethod mangle json string with escapes

Tag : json , By : rixtertech
Date : March 29 2020, 07:55 AM
like below fixes the issue first it would be nice to check two things, the body of the JSON object and verify that the string complies with a JSON structure.
I found errors in your initial string, the formatting that is valid in JSON is:
{"properties":{"template": {"contentVersion": "","$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","resources": {"type": "Microsoft.Network/networkSecurityGroups","name": "[parameters('GroupName')]","apiVersion":"2016-03-30","location":"[resourceGroup().Location]","properties": {"securityRules": ["@{name=DenyAll; properties=}"]}},"parameters":{"GroupName":{"defaultValue":"GroupName","type": "String"}}},"mode":"Incremental"}}
 {\"properties\": {\"template\": {\"contentVersion\": \"\",\"$schema\": \"https:\/\/schema.management.azure.com\/schemas\/2015-01-01\/deploymentTemplate.json#\",\"resources\": {\"type\": \"Microsoft.Network\/networkSecurityGroups\",\"name\": \"[parameters('GroupName')]\",\"apiVersion\": \"2016-03-30\",\"location\": \"[resourceGroup().Location]\",\"properties\": {\"securityRules\": [\"@{name=DenyAll; properties=}\"]}},\"parameters \": {\"GroupName\": {\"defaultValue\": \"GroupName\",\"type\": \"String\"}}},\"mode \": \"Incremental\"}}

`Invoke-RestMethod -Uri "..." -Method Get | select X,Y` doesn't return any row while `(Invoke-RestMethod -Uri

Tag : powershell , By : Pancilobak
Date : March 29 2020, 07:55 AM
should help you out I have a rest api which will return rows. However, , Generally speaking:
# Place (...) around the Invoke-RestMethod call to force enumeration.
(Invoke-RestMethod -Uri "..." -Method Get) | select XXX,YY
# Store array in a variable.
$array = Invoke-RestMethod -Uri "..." -Method Get

# An array stored in a variable sent through the pipeline is 
# invariably enumerated.
$array | select X,Y
# Send an array *as a whole* through the pipeline.
PS> (Write-Output -NoEnumerate (1..3) | Measure-Object).Count
1 # That is, the 3-element array was sent as *one* object

# Wrapping the command in (...) forces enumeration.
PS> ((Write-Output -NoEnumerate (1..3)) | Measure-Object).Count
3  # elements were sent *one by one*

# Store output-as-a-whole array in a variable,
# then send the variable through the pipeline - 
# which also forces enumeration.
PS> $array = Write-Output -NoEnumerate (1..3); ($array | Measure-Object).Count 
3  # elements were sent *one by one*
$array = 1..3

# Use Write-Output -NoEnumerate:
PS> (Write-Output -NoEnumerate $array | Measure-Object).Count
1 # array was sent *as a whole*

# Alternatively - faster and more concise, but more obscure -
# wrap the array in an aux. wrapper array, so that only 
# the wrapper array is enumerated, sending the original array
# as a whole:
PS> (, $array | Measure-Object).Count
1 # array was sent *as a whole*
Related Posts Related QUESTIONS :
  • How to convert value to KB, MB, or GB depending on digit placeholders?
  • How to copy files based on last modified date to network drive?
  • Remove-PSDrive doesn't remove Mapped Drives
  • Wait for Active Directory Authentication URL list to update within a Powershell Azure Function
  • Automating Telnet with PowerShell
  • How to suppress quotes in PowerShell commands to executables
  • Trying to move AD user and disable the account running some PowerShell
  • How to run a local powershell script into remote machine via ansible?
  • Set ACL System.Security.AccessControl.FileSystemAccessRule to multiple users?
  • Replace Part of Filename if Filename Already Exists
  • Using a function in a calculated field in Powershell?
  • How to download a file accepting license using powershell
  • Using Powershell, how to add a task sequence for installing an SCCM package
  • Powershell Azure function times out on processing queue
  • How to get a last 15 minutes of a formatted log using a powershell script
  • Using both desktop Powershell 5.1 and Powershell Core 6.1
  • What does mean % , $_ and @ in Powershell?
  • Teams: Invoke-Webrequest sending base64 string (png) to Teams
  • How can I replicate New-SmbGlobalMapping in C# code?
  • How do I output the files being copied in console?
  • Powershell: How to stop start-job?
  • Formatting output from PowerShell to a csv file
  • How to check if a PowerShell switch parameter is absent or false
  • How do I force a function to return a single element array instead of the contained object?
  • compressed file syntax can be supported across all powershell version
  • Is there a way in Powershell for a boolean parameter to automatically be true if not specified when the function is call
  • New-Partition -DriveLetter Option
  • How to add SAMAccountName to output?
  • How to do looping to rename and read a file using PowerShell?
  • Read CSV file then split the values and create a global variable
  • (Get-Date).ToUniversalTime() convert to string but not change format
  • Adding Extra Headers in CSV
  • New-Mailbox command does not accept the -Equipment argument
  • How do I remove IIS 'server' header from http response using Powershell?
  • Creating Subdirectory With Date As Today(mm-dd-yyyy) in Network Drive
  • My first parameter is not being passed to my function
  • How can I overwrite an existing zip file?
  • How to use double if statement using powershell?
  • Command using either or both -Path parameter and $input?
  • How can I return multiple values from a powershell script to the batch file that is calling it?
  • Sensitive word search with powershell
  • Copy files located in multiple subfolder, using a mildmatch search
  • Replace the text for all files in a Directory
  • How to write a PowerShell script which auto increments a number by 1 every time it's run?
  • new user powershell get-service
  • Removing Parts of a File Name based on a Delimiter
  • How can we transform Date in CSV file using PowerShell scripts
  • How to replace multiple characters (<3) of all the filenames in a folder (and subfolder) using PowerShell
  • Powershell environment variable
  • Removing spaces from a string using Powershell
  • List Filename and current date of all files in a directory
  • PowerShell calling environment variable with another variable
  • Uninstall all software starting with a specific string
  • Set-ADUser does not handle empty or null variables
  • How do I get the data results from Invoke-RestMethod into CSV file?
  • ConvertFrom-String returns symbol not text
  • Get-WinEvent Select time and field within data item of 1
  • Create multiple files with Powershell?
  • I have a folder that contains multiple folders which contain images. I want to filter the images based on dimensions
  • Function not working even though variable looks fine
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com