Hi @atanu !
First of all, I am sorry for the late response. Got caught up with some work & the holiday season..! Btw, Merry Christmas (I know it was a few days ago, but anyways..)!!
So, yes, it is possible to create entities using Postman. There are two ways!
- Using token (the way we were already doing it).
- As @chun_hing_yap suggested, using basic auth.
Let's see both of the approaches!
Before we begin with any of those approaches, I would like you to setup a new "Collection" (let's call it "ODK_Requests") in the Postman. Once done, go to the "Variables" section (or tab) of the "ODK_Requests" Collection and setup and save (yes! It is very important to save!) the following variables:
You will be adding "base_url" which will contain https://odkapi.capibuilder.org, "my_email" and "my_password" will contain your ODK Central email & pass. "project_id" will be 13 and "entity_name" will be people in your case.
Save it! And, follow along!
Approach 01: Using Token
- Create a new POST request (let's call it "Authenticate") in the ODK_Requests collection. This request will be the same one we were using to generate the token. It will look something like:
- Under request "Settings" tab, disable the cookie jar (the one I shared a short demo for).
- And, under the "Scripts" tab, add the following .js script in "Post-Response" section:
if (pm.response.code === 200) {
const jsonRes = pm.response.json();
const token = jsonRes.token;
pm.collectionVariables.set("authToken", token);
} else {
console.error("Request failed with status:", pm.response.code);
}
Will look something like:
- We are done, save the request and send it once. You should receive a token. Doing this should have automatically added a new variable (authToken) in "ODK_Requests" collection's Variable list (along with the one we added manually earlier).
- Create a new POST request, let's call it "AddEntity", it will look something like this:
- Under the "Authorization" tab in the "AddEntity" POST request, add a Bearer token variable (authToken - the one we generated above). It will look something like:
- Done! Save and send this request, a new entity record will be added to the dataset.
Note: It's enough to send an "Authenticate" request once every 24 hours (as each token is valid for 24 hours in general). You may proceed with sending multiple entity addition ("AddEntity") requests as convenient using a single token itself. You may also send multiple entity records to be added within a single request.
Alright! Let's check the second approach.
Approach 02: Using Basic Auth
- When using this, we can simply delete the "Authenticate" POST request we created / saved during the first approach.
- Under the "Authorization" tab in the "AddEntity" POST request, select the "Auth Type" as "Basic Auth", and add variables storing your ODK Central user and pass. It will look something like:
- Done! Save the request and send it as necessary!
Why two different alternative approaches and which one should you be using?
Well, it depends on the use-case. In general, we use the Bearer Token thing when we expect higher privacy or security in our request-response communication within the production environment, as it protects our credentials. And, we prefer using the Basic Auth thing in the development environment where we really aren't very concerned about the privacy or the security of our credentials maybe.
I guess, that would be all..!
Wishing you a very Happy New Year - 2025, in advance!
See ya! Let me know!