unicodeC / MeiliSearch v0.24 / Add or replace documents
Back to Collection Items
#include <C_CkHttpW.h>
#include <C_CkJsonArrayW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
HCkHttpW http;
BOOL success;
HCkJsonArrayW jarr;
HCkJsonObjectW jsonObj_1;
HCkStringBuilderW sbRequestBody;
HCkHttpResponseW resp;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// 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"
// }
// ]
jarr = CkJsonArrayW_Create();
CkJsonArrayW_AddObjectAt(jarr,-1);
jsonObj_1 = CkJsonArrayW_ObjectAt(jarr,CkJsonArrayW_getSize(jarr) - 1);
CkJsonObjectW_UpdateInt(jsonObj_1,L"id",2);
CkJsonObjectW_UpdateString(jsonObj_1,L"title",L"Pride and Prejudice");
CkJsonObjectW_UpdateString(jsonObj_1,L"author",L"Jane Austin");
CkJsonObjectW_UpdateString(jsonObj_1,L"genre",L"romance");
CkJsonObjectW_UpdateNumber(jsonObj_1,L"price",L"3.5");
CkJsonObjectW_Dispose(jsonObj_1);
CkJsonArrayW_AddObjectAt(jarr,-1);
jsonObj_1 = CkJsonArrayW_ObjectAt(jarr,CkJsonArrayW_getSize(jarr) - 1);
CkJsonObjectW_UpdateInt(jsonObj_1,L"id",456);
CkJsonObjectW_UpdateString(jsonObj_1,L"title",L"Le Petit Prince");
CkJsonObjectW_UpdateString(jsonObj_1,L"author",L"Antoine de Saint-Exupéry");
CkJsonObjectW_UpdateString(jsonObj_1,L"genre",L"adventure");
CkJsonObjectW_UpdateNumber(jsonObj_1,L"price",L"10.0");
CkJsonObjectW_Dispose(jsonObj_1);
CkJsonArrayW_AddObjectAt(jarr,-1);
jsonObj_1 = CkJsonArrayW_ObjectAt(jarr,CkJsonArrayW_getSize(jarr) - 1);
CkJsonObjectW_UpdateInt(jsonObj_1,L"id",1);
CkJsonObjectW_UpdateString(jsonObj_1,L"title",L"Alice In Wonderland");
CkJsonObjectW_UpdateString(jsonObj_1,L"author",L"Lewis Carroll");
CkJsonObjectW_UpdateString(jsonObj_1,L"genre",L"fantasy");
CkJsonObjectW_UpdateNumber(jsonObj_1,L"price",L"25.99");
CkJsonObjectW_Dispose(jsonObj_1);
CkJsonArrayW_AddObjectAt(jarr,-1);
jsonObj_1 = CkJsonArrayW_ObjectAt(jarr,CkJsonArrayW_getSize(jarr) - 1);
CkJsonObjectW_UpdateInt(jsonObj_1,L"id",1344);
CkJsonObjectW_UpdateString(jsonObj_1,L"title",L"The Hobbit");
CkJsonObjectW_UpdateString(jsonObj_1,L"author",L"J. R. R. Tolkien");
CkJsonObjectW_UpdateString(jsonObj_1,L"genre",L"fantasy");
CkJsonObjectW_Dispose(jsonObj_1);
CkJsonArrayW_AddObjectAt(jarr,-1);
jsonObj_1 = CkJsonArrayW_ObjectAt(jarr,CkJsonArrayW_getSize(jarr) - 1);
CkJsonObjectW_UpdateInt(jsonObj_1,L"id",4);
CkJsonObjectW_UpdateString(jsonObj_1,L"title",L"Harry Potter and the Half-Blood Prince");
CkJsonObjectW_UpdateString(jsonObj_1,L"author",L"J. K. Rowling");
CkJsonObjectW_UpdateString(jsonObj_1,L"genre",L"fantasy");
CkJsonObjectW_Dispose(jsonObj_1);
CkJsonArrayW_AddObjectAt(jarr,-1);
jsonObj_1 = CkJsonArrayW_ObjectAt(jarr,CkJsonArrayW_getSize(jarr) - 1);
CkJsonObjectW_UpdateInt(jsonObj_1,L"id",42);
CkJsonObjectW_UpdateString(jsonObj_1,L"title",L"The Hitchhiker's Guide to the Galaxy");
CkJsonObjectW_UpdateString(jsonObj_1,L"author",L"Douglas Adams");
CkJsonObjectW_Dispose(jsonObj_1);
CkHttpW_SetRequestHeader(http,L"X-Meili-Api-Key",L"masterKey");
sbRequestBody = CkStringBuilderW_Create();
CkJsonArrayW_EmitSb(jarr,sbRequestBody);
resp = CkHttpW_PTextSb(http,L"POST",L"http://localhost:7700/indexes/indexUID/documents",sbRequestBody,L"utf-8",L"application/json",FALSE,FALSE);
if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonArrayW_Dispose(jarr);
CkStringBuilderW_Dispose(sbRequestBody);
return;
}
wprintf(L"%d\n",CkHttpResponseW_getStatusCode(resp));
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
CkHttpResponseW_Dispose(resp);
CkHttpW_Dispose(http);
CkJsonArrayW_Dispose(jarr);
CkStringBuilderW_Dispose(sbRequestBody);
}
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": [
]
}