Chilkat Online Tools

PureBasic / Support API / List Search Results

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::setCkLogin(http, "login")
    CkHttp::setCkPassword(http, "password")

    queryParams.i = CkJsonObject::ckCreate()
    If queryParams.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(queryParams,"query","<string>")
    CkJsonObject::ckUpdateString(queryParams,"sort_by","<string>")
    CkJsonObject::ckUpdateString(queryParams,"sort_order","<string>")

    CkHttp::ckSetRequestHeader(http,"Accept","application/json")

    resp.i = CkHttp::ckQuickRequestParams(http,"GET","https://example.zendesk.com/api/v2/search",queryParams)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(queryParams)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(queryParams)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "count": "<integer>",
    ;   "facets": "<string>",
    ;   "next_page": "<string>",
    ;   "previous_page": "<string>",
    ;   "results": [
    ;     {
    ;       "created_at": "<string>",
    ;       "default": "<boolean>",
    ;       "deleted": "<boolean>",
    ;       "description": "<string>",
    ;       "id": "<integer>",
    ;       "name": "<string>",
    ;       "result_type": "<string>",
    ;       "updated_at": "<string>",
    ;       "url": "<string>"
    ;     },
    ;     {
    ;       "created_at": "<string>",
    ;       "default": "<boolean>",
    ;       "deleted": "<boolean>",
    ;       "description": "<string>",
    ;       "id": "<integer>",
    ;       "name": "<string>",
    ;       "result_type": "<string>",
    ;       "updated_at": "<string>",
    ;       "url": "<string>"
    ;     }
    ;   ]
    ; }

    ; Sample code for parsing the JSON response...
    ; Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    created_at.s
    default.s
    deleted.s
    description.s
    id.s
    name.s
    result_type.s
    updated_at.s
    url.s

    count.s = CkJsonObject::ckStringOf(jResp,"count")
    facets.s = CkJsonObject::ckStringOf(jResp,"facets")
    next_page.s = CkJsonObject::ckStringOf(jResp,"next_page")
    previous_page.s = CkJsonObject::ckStringOf(jResp,"previous_page")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"results")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        created_at = CkJsonObject::ckStringOf(jResp,"results[i].created_at")
        default = CkJsonObject::ckStringOf(jResp,"results[i].default")
        deleted = CkJsonObject::ckStringOf(jResp,"results[i].deleted")
        description = CkJsonObject::ckStringOf(jResp,"results[i].description")
        id = CkJsonObject::ckStringOf(jResp,"results[i].id")
        name = CkJsonObject::ckStringOf(jResp,"results[i].name")
        result_type = CkJsonObject::ckStringOf(jResp,"results[i].result_type")
        updated_at = CkJsonObject::ckStringOf(jResp,"results[i].updated_at")
        url = CkJsonObject::ckStringOf(jResp,"results[i].url")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(queryParams)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl  -u login:password -G -d "query=%3Cstring%3E"
	-d "sort_by=%3Cstring%3E"
	-d "sort_order=%3Cstring%3E"
	-H "Accept: application/json"
https://example.zendesk.com/api/v2/search

Postman Collection Item JSON

{
  "name": "List Search Results",
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "url": {
      "raw": "{{baseUrl}}/api/v2/search?query=<string>&sort_by=<string>&sort_order=<string>",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "api",
        "v2",
        "search"
      ],
      "query": [
        {
          "key": "query",
          "value": "<string>",
          "description": "(Required) The search query. See [Query basics](#query-basics) above. For details on the query syntax, see the [Zendesk Support search reference](https://support.zendesk.com/hc/en-us/articles/203663226)"
        },
        {
          "key": "sort_by",
          "value": "<string>",
          "description": "One of `updated_at`, `created_at`, `priority`, `status`, or `ticket_type`. Defaults to sorting by relevance"
        },
        {
          "key": "sort_order",
          "value": "<string>",
          "description": "One of `asc` or `desc`.  Defaults to `desc`"
        }
      ]
    },
    "description": "Use the ampersand character (&) to append the `sort_by` or `sort_order` parameters to the URL.\n\nFor examples, see [Searching with Zendesk API](/documentation/ticketing/using-the-zendesk-api/searching-with-the-zendesk-api).\n\n#### Pagination\n\n* Offset pagination only\n\nOffset pagination may result in duplicate results when paging. You can also use the \n[Export Search Results](/api-reference/ticketing/ticket-management/search/#export-search-results) endpoint, which \nuses cursor-based pagination and doesn't return duplicate results. See \n[Pagination](/api-reference/introduction/pagination/) for more information.\n\n#### Allowed For\n\n* Admins, Agents and Light Agents\n\n\n#### Errors JSON Format\n\nErrors are represented as JSON objects which have the following keys:\n\n| Name                  | Type                 | Comment\n| --------------------- | ---------------------| --------------------\n| error                 | string               | The type of error. Examples: \"unavailable\", \"invalid\"\n| description           | string               |\n\n##### Example Error\n```js\n{\n  \"error\": \"unavailable\",\n  \"description\": \"Sorry, we could not complete your search query. Please try again in a moment.\"\n}\n```\n"
  },
  "response": [
    {
      "name": "Success response",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "description": "Added as a part of security scheme: basic",
            "key": "Authorization",
            "value": "Basic <credentials>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/api/v2/search?query=<string>&sort_by=<string>&sort_order=<string>",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "api",
            "v2",
            "search"
          ],
          "query": [
            {
              "key": "query",
              "value": "<string>",
              "description": "(Required) The search query. See [Query basics](#query-basics) above. For details on the query syntax, see the [Zendesk Support search reference](https://support.zendesk.com/hc/en-us/articles/203663226)"
            },
            {
              "key": "sort_by",
              "value": "<string>",
              "description": "One of `updated_at`, `created_at`, `priority`, `status`, or `ticket_type`. Defaults to sorting by relevance"
            },
            {
              "key": "sort_order",
              "value": "<string>",
              "description": "One of `asc` or `desc`.  Defaults to `desc`"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"count\": \"<integer>\",\n  \"facets\": \"<string>\",\n  \"next_page\": \"<string>\",\n  \"previous_page\": \"<string>\",\n  \"results\": [\n    {\n      \"created_at\": \"<string>\",\n      \"default\": \"<boolean>\",\n      \"deleted\": \"<boolean>\",\n      \"description\": \"<string>\",\n      \"id\": \"<integer>\",\n      \"name\": \"<string>\",\n      \"result_type\": \"<string>\",\n      \"updated_at\": \"<string>\",\n      \"url\": \"<string>\"\n    },\n    {\n      \"created_at\": \"<string>\",\n      \"default\": \"<boolean>\",\n      \"deleted\": \"<boolean>\",\n      \"description\": \"<string>\",\n      \"id\": \"<integer>\",\n      \"name\": \"<string>\",\n      \"result_type\": \"<string>\",\n      \"updated_at\": \"<string>\",\n      \"url\": \"<string>\"\n    }\n  ]\n}"
    }
  ]
}