Error when sending message to AppDynamics through powerhsell using invoke-restmethod - powershell

I am trying to send a message to App Dynamics using 'Invoke-RestMethod' through powershell which is giving below error
Invoke-RestMethod : AppDynamics - Error report HTTP Status 400 - Event
summary is not specifiedtype Status reportmessageEvent summary is not
specifieddescriptionThe request sent by the client was syntactically
incorrect.
I am using below code to send message.
$JSONBody = #{
'#context'= 'http://schema.org/extensions'
'#type'= 'MessageCard'
'title' = 'Incoming Alert Test Message'
'text' = 'xyz'
'eventtype'='CUSTOM'
'customeventtype'='appDcustomevent'
}
$json = ConvertTo-Json $JSONBody -Depth 100
$headers = #{Authorization='Basic '+[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('username#account:password'))}
$response = Invoke-RestMethod -Uri 'https://rest api url/events' -Proxy 'proxy url:80' -Method Post -Headers $headers -Body $json -ContentType 'application/json'
Please help me understand how to fix this issue as i have no clue.
Thanks in advance,
Usha.

Your request is missing the "summary" field as per the documentation: https://docs.appdynamics.com/appd/20.x/en/extend-appdynamics/appdynamics-apis/alert-and-respond-api/events-and-action-suppression-api#EventsandActionSuppressionAPI-CreateaCustomEvent
(Every field marked as Mandatory under "Input parameters" table must be included in the request)
Update:
Below is tested as working, seems the JSON version does indeed have issues - so switched to using query params and this works as intended.
$application_id = "<APPLICATION_NAME>"
$summary = "This_is_a_summary"
$severity = "INFO"
$eventtype = "CUSTOM"
$controller = "<CONTROLLER_URL_NO_PROTOCOL>"
$port = "8090"
$protocol = "http"
$account = "<ACCOUNT>"
$username = "<USERNAME>"
$password = "<PASSWORD>"
$controllerEndpoint = "controller/rest/applications/${application_id}/events"
$restURL = "${protocol}://${controller}:${port}/${controllerEndpoint}?severity=${severity}&summary=${summary}&eventtype=${eventtype}"
"$restURL"
$headers = #{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${username}#${account}:${password}"))}
$response = Invoke-RestMethod -Uri $restURL -Method Post -Headers $headers -Body $JSON -ContentType "application/json"
$response.content

Related

Invoke-RestMethod Get Request with Query Parameters

I was tasked with spinning up a script for a client which relied on a basic 'GET' request to an API which returned a JSON object which I used info from that to make subsequent calls. This worked great but requirements changed and now I need to send the request with some parameters.
Did some testing in postman and the call works great when I add the query parameters at the end of the Uri (ie. https://test.com/?type=image) but when I try to alter the Uri in the Invoke-WebRequest I'm getting a 'Invoke-RestMethod : Invalid or expired token' error. When I take out the parameters the it works as expected, just with incorrect data.
I have also tried turning the query parameters into a hashtable and as json, and sending it as the body but still get the same error.
I'm at the end of my rope and any insight is appreciated.
what works
$baseUrl = 'https://test.com/api/v2/'
$method = 'GET'
$auth = Get-PSAuthorizationString -Uri $baseUrl -OauthConsumerKey $oauth_consumer_key -OauthConsumerSecret $oauth_consumer_secret -OauthAccessToken $oauth_token -OauthAccessTokenSecret $oauth_token_secret
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", $auth)
$responses = Invoke-RestMethod -Method $method -Headers $headers -Uri $baseUrl
what breaks it
$baseUrl = 'https://test.com/api/v2/?type=image'
$responses = Invoke-RestMethod -Method $method -Headers $headers -Uri $baseUrl
$body = #{}
$body['type']="image"
$responses = Invoke-RestMethod -Method $method -Headers $headers -Uri $baseUrl -body $body
Looks like you are inadvertently using $baseUrl in 2 different places, when requesting your token and when invoking your web request.
From your error message, I guess the authorization service does not tolerate the extra parameter.
Try this simple change:
$authUrl = 'https://test.com/api/v2/'
$baseUrl = 'https://test.com/api/v2/?type=image'
$method = 'GET'
$auth = Get-PSAuthorizationString -Uri $authUrl -OauthConsumerKey $oauth_consumer_key -OauthConsumerSecret $oauth_consumer_secret -OauthAccessToken $oauth_token -OauthAccessTokenSecret $oauth_token_secret
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", $auth)
$responses = Invoke-RestMethod -Method $method -Headers $headers -Uri $baseUrl
Hope this helps!

graph api assign manager

I'm trying to assign the manager to a user in AAD the documentation says
PUT /users/{id}/manager/$ref
but i'm not sure what to feed the $ref variable. I've tried UPN and the ID, but I keep getting
The remote server returned an error: (400) Bad Request.
Here is how i'm trying to put the manager info, but clearly i'm not doing it right or I can't read the documentation from here
$Header = #{
Authorization = "$($Request.token_type) $($Request.access_token)"
}
$bodyProcess = #{
id= "string aa9999a1-1111-11a2-abab-asfdas32"
}
$body = $bodyProcess | ConvertTo-Json
$Uri = "https://graph.microsoft.com/v1.0/users/4d5f6c5a-0e69-40b6-a86d-e825582add50/manager/$ref"
$UserData = Invoke-RestMethod -Uri $Uri -Headers $Header -Method PUT -ContentType "application/json" -Body $Body
Any help would be greatly appreciated.
thanks,
Here is the full script that works for me.
$Header = #{
Authorization = "$($Request.token_type) $($Request.access_token)"
}
$bodyProcess = #{
"#odata.id"= "https://graph.microsoft.com/v1.0/users/aa9999a1-1111-11a2-abab-asfdas32"
}
$body = $bodyProcess | ConvertTo-Json
$Uri = 'https://graph.microsoft.com/v1.0/users/4d5f6c5a-0e69-40b6-a86d-e825582add50/manager/$ref'
Invoke-RestMethod -Uri $Uri -Headers $Header -Method PUT -ContentType "application/json" -Body $Body

Invoke-WebRequest -Method 'POST' with -ContentType 'application/json' Fails

When using:
$body = #{
Manager = "spmigrationuser#contoso.com" #$item.PMEmail
Name = "some name"
Number = "Some number"
Practice = "Some Practice"
}
$response = Invoke-RestMethod -Method Post -Uri $Url -Body $body -ContentType 'application/json' # -Headers $Headers
Or
$response = Invoke-WebRequest -Method 'POST' -Uri $Url -Body $body -ContentType 'application/json' # -Headers $Headers
Neither ContentType 'application/json'
Nor
$Headers = #{'Content-Type' = 'application/json' }
-Headers $Headers
Works
The error is always:
"Invoke-WebRequest : {"error":{"code":"InvalidRequestContent","message":"The request content is not valid and could not be deserialized: 'Error parsing NaN value. Path '', line 1, position 1.'."}}"
The same call works in Postman
I am using PS 5.1 and I must have -ContentType 'application/json' otherwise PS works but the service fails
What can be the issue?
I agree with NickSalacious. Your issue is that you are not sending JSON.
If you are using Postman and just starting to do API in PowerShell. Postman has a "Code" Link in the top right hand corner of the request. Just below and to the right of the Send button. In there you can select PowerShell. This will give you a good basis to see how the same request could be ran in PowerShell.
Postman would turn your body into this:
$body = "{`n `"Manager`": `"spmigrationuser#contoso.com`",`n `"Name`": `"some name`",`n `"Number`": `"Some number`",`n `"Practice`": `"Some Practice`"`n}"
This is not the easiest to work with and to read. Learning and using ConvertTo-Json is going to help a lot more in the long run.
*Edit: Also look at Invoke-RestMethod and Invoke-WebRequest. They behave differently and sometimes one will be better than the other.
*Edit2: Figured I would put an example of another way to do it.
$request = #{
Uri = 'http://YourURI.Here'
Headers = #{ 'Authorization' = $token
'AnotherHeader?' = 'Sure'}
Method = 'POST'
Body = '{
"Manager": $item.PMEmail,
"Name": "some name",
"Number": "Some number",
"Practice": "Some Practice"
}'
ContentType = 'application/json'
}
$response = Invoke-RestMethod #request
The API requires that the body be a JSON string. You can do a simple conversion (using ConvertTo-Json) in your Invoke-RestMethod command and set the content type accordingly.
Invoke-RestMethod -Method POST -Uri $uri -Header $header -Body ($body | ConvertTo-Json) -ContentType 'application/json'
Sorry I bothered all of you.
I tested on another computer and it works fine.

Facing issue while getting response of API in Powershell

I trying to create a Powershell Script to check that my API's are working or not. I am able to hit the API using Invoke-RestMethod without any issue but I am unable to get API response.
I have tried Invoke-RestMethod and Invoke-WebRequest
$user = 'User'
$pass = 'Test123'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = #{
Authorization = $basicAuthValue
}
$url="MYAPIURL"
$person = #{
UserID='test'
Password='Test123'
}
$json = $person | ConvertTo-Json
$Result =Invoke-RestMethod -Uri $url -Headers $Headers -Body $json -Method Post -ContentType 'application/json'
Write-Host ($Result | Format-Table | Out-String)
Always getting blank in $Result. In Postman it's working fine.

PowerShell 403 HTTP Request With Parameters Containing Square Brackets

I'm trying to make a PATCH request using the following lines in PowerShell but it is returning a 403:
Try{
$Body = #{
'api_key' = 'myapikey'
'item[status]' = 'unavailable'
} | ConvertTo-Json
$response = Invoke-WebRequest -Method PATCH -Uri "https://api.example.com/store/apikey.json" -Body $Body -ContentType "application/json"
$response.StatusCode
}
Catch [System.Net.WebException]{
$code = [int]$_.Exception.Response.StatusCode
}
Fiddler is returning a 403 with the following message: "{"error":"Parameter item is required"}". Also, the query string in Fiddler is empty. However, a successful request is made when everything is hard-coded into the Uri:
$statusUpdate = Invoke-WebRequest -Method PATCH -Uri "https://api.example.com/store/apikey.json?api_key=myapikey&item[status]=unavailable" -ContentType "application/json"
If that's the code you're actually using to hit the API, your problem appears to simply be due to piping your json body to out-host, thus sending an empty $Body to the API, so you should edit it to do this:
try
{
$Body = #{
'api_key' = 'myapikey'
'item[status]' = 'unavailable'
} | ConvertTo-Json
$response = Invoke-WebRequest -Method PATCH -Uri "https://api.example.com/store/apikey.json" -Body $Body -ContentType "application/json"
$response.StatusCode
}
catch [System.Net.WebException]
{
$code = [int]$_.Exception.Response.StatusCode
}