Search code examples
powershellapicurlgetodata

API GET Request works fine with curl, but with powershell not


Good afternoon,

If I send this curl request via a command line it works fine. But when I use my powershell script I don't get the same result.

CURL script:

CURL --request GET "https://staging.tiptrack.nl/Tiptrack.Employer.Api/odata/EmployeeBudgets?$expand=Employee($expand=SecureEmployee)&$top=5"  -H "accept: application/json"  -H "Authorization: Bearer token"

Powershell script:

#------- Opvragen token tiptrack -------
#Dit is de URL waar de token voor tiptrack wordt opgevraagd.
$Url_token="https://tiptracknext-staging-login.indicia.nl/oauth2/aus342go9hNphcHXM0i7/v1/token"

#Dit is de body die mee wordt gestuurd in de request, deze informatie staat gelijk aan de data in de post request vanuit de handleiding.
$Data_token = @{
grant_type="client_credentials"
client_id="123456"
client_secret="123456"
scope="api"
}

$token_tiptrack=Invoke-RestMethod -Method Post -Uri $Url_token -ContentType "application/x-www-form-urlencoded" -Body $Data_token

#------- Opvragen Employerbudgetsid -------
#Dti is de URL waarna de GET request wordt gestuurd om het employerid te kunnen.
$Url_budgetid="https://staging.tiptrack.nl/Tiptrack.Employer.Api/odata/EmployeeBudgets?$expand=Employee($expand=SecureEmployee)&$top=5"

#Dit is header die mee wordt gestuurd in de request. Deze data in deze header staat gelijk aan de data in de API handleiding.
$header_process = @{
Authorization='Bearer '+$token_tiptrack.access_token
"accept"="application/json"
}

#Vanuit het uploaden van het bestand krijgen we een reactie van de server, in deze reactie staat het upload id, deze id hebben we nodig om het bestand te kunnen verwerken. 
Invoke-RestMethod -Uri $Url_budgetid -Method Get -Headers $header_process | Select-Object -ExpandProperty value

I hope someone can help me with this problem. With the CURL action i get the first 5 rows and by powershell i get all avilible rows.


Solution

  • To pass a string value as verbatim or literal, it is favorable to use single quotes or backtick escape PowerShell's special characters. If you have no variable references within a string, single quotes is easiest.

    # Using Single Quotes
    $Url_budgetid='https://staging.tiptrack.nl/Tiptrack.Employer.Api/odata/EmployeeBudgets?$expand=Employee($expand=SecureEmployee)&$top=5'
    
    # Escaping the $ while using double quotes
    $Url_budgetid="https://staging.tiptrack.nl/Tiptrack.Employer.Api/odata/EmployeeBudgets?`$expand=Employee(`$expand=SecureEmployee)&`$top=5"
    

    Using double quotes to surround a string makes the string expandable. When the code is run $ followed by legal variable name characters will be interpreted as variable references. In your session, $expand and $top would be substituted for their values, which would be $null if you had not defined them. As a result, it appears those strings are removed from the URI. You can see this happening just by typing $Url_budgetid at the console.