Chilkat Online Tools

DataFlex / MeiliSearch v0.24 / Add or replace documents

Back to Collection Items

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Handle hoJarr
    Variant vJsonObj_1
    Handle hoJsonObj_1
    Variant vSbRequestBody
    Handle hoSbRequestBody
    Variant vResp
    Handle hoResp
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // [
    //   {
    //     "id": 2,
    //     "title": "Pride and Prejudice",
    //     "author": "Jane Austin",
    //     "genre": "romance",
    //     "price": 3.5
    //   },
    //   {
    //     "id": 456,
    //     "title": "Le Petit Prince",
    //     "author": "Antoine de Saint-Exupéry",
    //     "genre": "adventure",
    //     "price": 10.0
    //   },
    //   {
    //     "id": 1,
    //     "title": "Alice In Wonderland",
    //     "author": "Lewis Carroll",
    //     "genre": "fantasy",
    //     "price": 25.99
    //   },
    //   {
    //     "id": 1344,
    //     "title": "The Hobbit",
    //     "author": "J. R. R. Tolkien",
    //     "genre": "fantasy"
    //   },
    //   {
    //     "id": 4,
    //     "title": "Harry Potter and the Half-Blood Prince",
    //     "author": "J. K. Rowling",
    //     "genre": "fantasy"
    //   },
    //   {
    //     "id": 42,
    //     "title": "The Hitchhiker's Guide to the Galaxy",
    //     "author": "Douglas Adams"
    //   }
    // ]

    Get Create (RefClass(cComChilkatJsonArray)) To hoJarr
    If (Not(IsComObjectCreated(hoJarr))) Begin
        Send CreateComObject of hoJarr
    End

    Get ComAddObjectAt Of hoJarr -1 To iSuccess
    Get ComSize Of hoJarr To iTemp1
    Get ComObjectAt Of hoJarr (iTemp1 - 1) To vJsonObj_1
    If (IsComObject(vJsonObj_1)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonObj_1
        Set pvComObject Of hoJsonObj_1 To vJsonObj_1
    End
    Get ComUpdateInt Of hoJsonObj_1 "id" 2 To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "title" "Pride and Prejudice" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "author" "Jane Austin" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "genre" "romance" To iSuccess
    Get ComUpdateNumber Of hoJsonObj_1 "price" "3.5" To iSuccess
    Send Destroy of hoJsonObj_1

    Get ComAddObjectAt Of hoJarr -1 To iSuccess
    Get ComSize Of hoJarr To iTemp1
    Get ComObjectAt Of hoJarr (iTemp1 - 1) To vJsonObj_1
    If (IsComObject(vJsonObj_1)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonObj_1
        Set pvComObject Of hoJsonObj_1 To vJsonObj_1
    End
    Get ComUpdateInt Of hoJsonObj_1 "id" 456 To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "title" "Le Petit Prince" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "author" "Antoine de Saint-Exupéry" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "genre" "adventure" To iSuccess
    Get ComUpdateNumber Of hoJsonObj_1 "price" "10.0" To iSuccess
    Send Destroy of hoJsonObj_1

    Get ComAddObjectAt Of hoJarr -1 To iSuccess
    Get ComSize Of hoJarr To iTemp1
    Get ComObjectAt Of hoJarr (iTemp1 - 1) To vJsonObj_1
    If (IsComObject(vJsonObj_1)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonObj_1
        Set pvComObject Of hoJsonObj_1 To vJsonObj_1
    End
    Get ComUpdateInt Of hoJsonObj_1 "id" 1 To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "title" "Alice In Wonderland" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "author" "Lewis Carroll" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "genre" "fantasy" To iSuccess
    Get ComUpdateNumber Of hoJsonObj_1 "price" "25.99" To iSuccess
    Send Destroy of hoJsonObj_1

    Get ComAddObjectAt Of hoJarr -1 To iSuccess
    Get ComSize Of hoJarr To iTemp1
    Get ComObjectAt Of hoJarr (iTemp1 - 1) To vJsonObj_1
    If (IsComObject(vJsonObj_1)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonObj_1
        Set pvComObject Of hoJsonObj_1 To vJsonObj_1
    End
    Get ComUpdateInt Of hoJsonObj_1 "id" 1344 To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "title" "The Hobbit" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "author" "J. R. R. Tolkien" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "genre" "fantasy" To iSuccess
    Send Destroy of hoJsonObj_1

    Get ComAddObjectAt Of hoJarr -1 To iSuccess
    Get ComSize Of hoJarr To iTemp1
    Get ComObjectAt Of hoJarr (iTemp1 - 1) To vJsonObj_1
    If (IsComObject(vJsonObj_1)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonObj_1
        Set pvComObject Of hoJsonObj_1 To vJsonObj_1
    End
    Get ComUpdateInt Of hoJsonObj_1 "id" 4 To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "title" "Harry Potter and the Half-Blood Prince" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "author" "J. K. Rowling" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "genre" "fantasy" To iSuccess
    Send Destroy of hoJsonObj_1

    Get ComAddObjectAt Of hoJarr -1 To iSuccess
    Get ComSize Of hoJarr To iTemp1
    Get ComObjectAt Of hoJarr (iTemp1 - 1) To vJsonObj_1
    If (IsComObject(vJsonObj_1)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoJsonObj_1
        Set pvComObject Of hoJsonObj_1 To vJsonObj_1
    End
    Get ComUpdateInt Of hoJsonObj_1 "id" 42 To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "title" "The Hitchhiker's Guide to the Galaxy" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "author" "Douglas Adams" To iSuccess
    Send Destroy of hoJsonObj_1

    Send ComSetRequestHeader To hoHttp "X-Meili-Api-Key" "masterKey"

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequestBody
    If (Not(IsComObjectCreated(hoSbRequestBody))) Begin
        Send CreateComObject of hoSbRequestBody
    End
    Get pvComObject of hoSbRequestBody to vSbRequestBody
    Get ComEmitSb Of hoJarr vSbRequestBody To iSuccess

    Get pvComObject of hoSbRequestBody to vSbRequestBody
    Get ComPTextSb Of hoHttp "POST" "http://localhost:7700/indexes/indexUID/documents" vSbRequestBody "utf-8" "application/json" False False To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln iTemp1
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1
    Send Destroy of hoResp


End_Procedure

Curl Command

curl -X POST
	-H "X-Meili-Api-Key: masterKey"
	-d '[
  { "id": 2,    "title": "Pride and Prejudice",                    "author": "Jane Austin",              "genre": "romance",    "price": 3.5 },
  { "id": 456,  "title": "Le Petit Prince",                        "author": "Antoine de Saint-Exupéry", "genre": "adventure" , "price": 10.0 },
  { "id": 1,    "title": "Alice In Wonderland",                    "author": "Lewis Carroll",            "genre": "fantasy",    "price": 25.99 },
  { "id": 1344, "title": "The Hobbit",                             "author": "J. R. R. Tolkien",         "genre": "fantasy" },
  { "id": 4,    "title": "Harry Potter and the Half-Blood Prince", "author": "J. K. Rowling",            "genre": "fantasy" },
  { "id": 42,   "title": "The Hitchhiker\'s Guide to the Galaxy",   "author": "Douglas Adams" }
]'
http://localhost:7700/indexes/indexUID/documents

Postman Collection Item JSON

{
  "name": "Add or replace documents",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n  { \"id\": 2,    \"title\": \"Pride and Prejudice\",                    \"author\": \"Jane Austin\",              \"genre\": \"romance\",    \"price\": 3.5 },\n  { \"id\": 456,  \"title\": \"Le Petit Prince\",                        \"author\": \"Antoine de Saint-Exupéry\", \"genre\": \"adventure\" , \"price\": 10.0 },\n  { \"id\": 1,    \"title\": \"Alice In Wonderland\",                    \"author\": \"Lewis Carroll\",            \"genre\": \"fantasy\",    \"price\": 25.99 },\n  { \"id\": 1344, \"title\": \"The Hobbit\",                             \"author\": \"J. R. R. Tolkien\",         \"genre\": \"fantasy\" },\n  { \"id\": 4,    \"title\": \"Harry Potter and the Half-Blood Prince\", \"author\": \"J. K. Rowling\",            \"genre\": \"fantasy\" },\n  { \"id\": 42,   \"title\": \"The Hitchhiker's Guide to the Galaxy\",   \"author\": \"Douglas Adams\" }\n]",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{url}}/indexes/{{indexUID}}/documents",
      "host": [
        "{{url}}"
      ],
      "path": [
        "indexes",
        "{{indexUID}}",
        "documents"
      ],
      "query": [
        {
          "key": "primaryKey",
          "value": "id",
          "disabled": true
        }
      ]
    }
  },
  "response": [
  ]
}