Announcement

Collapse
No announcement yet.

API Request in Powershell with "where" filtering

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • API Request in Powershell with "where" filtering

    Hi there,

    I am new in the Espo-World. I am trying to write a GET-Request, to get an user id of a contact, filtered by emailAddress. I found the following information, that I can filter the contacts by emailAddress: https://docs.espocrm.com/development...s/#where-items

    But I can't find any example, how to do it in Powershell. Every try results in a 500 respond from the API.


    $whereFilter = "{
    'type':'equals',
    'attribute':'emailAddress',
    'value':'+$mail+'
    }"

    $compressedWhereFilter = $whereFilter | ConvertFrom-Json | ConvertTo-Json -Depth 10 -Compress

    $headers = New-Object"System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("X-Api-Key", $xApiKey)

    [System.Web.HttpUtility]::UrlEncode($CompressedFilters)

    $encodedUri = "{0}Contact?where={1}"-f$baseUrl,[System.Web.HttpUtility]::UrlEncode($compressedWhereFilter)

    return Invoke-RestMethod -Uri $encodedUri -Method 'GET' -Headers $headers | ConvertTo-Json

    That is my last try. Without UrlEncode I get 500 too.

    I would appreciate any help.

    Greeting from Germany
    Paul

    >>>> EDIT:

    I think I found a good solution:
    $headers = New-Object"System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("X-Api-Key", $xApiKey)

    $url = $baseUrl+"Contact?where[0][type]=equals&where[0][attribute]=emailAddress&where[0][value]="+$mail
    returnInvoke-RestMethod -Uri $url -Method 'GET' -Headers $headers

    Here I got the idea to look at the ready URI to filter the contacts: https://www.youtube.com/watch?v=j6NZ41Uj7R8
    Dzięki za video i pozdrowienia, emillod
    Last edited by paul; 04-09-2022, 07:40 PM. Reason: I found the solution, how it works for me.

  • #2
    Thanks for posting the solution.

    Comment


    • #3
      As of 7.0.0 search parameters can be passed in JSON format in the parameter `searchParams`. So it's much easier, not requiring building `where` arrays.

      Comment

    Working...
    X