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?
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;
try
{
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": "1.0.0.0","$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\": \"1.0.0.0\",\"$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
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*
|