# Postman for login post API returning 'could not authenticate'

**URL:** <https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687>\
**Category:** Support\
**Tags:** odk-central\
**Created:** [December 23, 2024, 4:35pm UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687 "2024-12-23T16:35:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![atanu](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/atanu/32/18622_2.png) [@atanu](https://forum.getodk.org/u/atanu)\
**Post date:** [December 23, 2024, 4:35pm UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/1 "2024-12-23T16:35:12Z")

</div>

**1. What is the issue? Please be detailed.**  
Using postman to send the login request to https://{{ODK\_SERVER}}/v1/sessions.  
Body (JSON):  
{  
"email": "EMAIL\_ID",  
"password": "PASSWORD"  
}

This worked earlier, and i saved it as part of the Postman collection.

The response i am getting now is  
{  
"message": "Could not authenticate with the provided credentials.",  
"code": 401.2  
}

The same credential is working fine for web login.

**2. What steps can we take to reproduce this issue?**  
Not sure.

**3. What have you tried to fix the issue?**  
Use a different cred but still the same response.

**4. Upload any forms or screenshots you can share publicly below.**

 ![CleanShot 2024-12-23 at 22.03.13@2x](https://getodk.b-cdn.net/uploads/default/original/3X/8/a/8a2a92445579120f0a3e143032c099edfbedfd38.png)

---

<div class="post-metadata">

**Author:** ![MinimalPotato](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/minimalpotato/32/30174_2.png) [@MinimalPotato](https://forum.getodk.org/u/MinimalPotato)\
**Post date:** [December 24, 2024, 4:59am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/2 "2024-12-24T04:59:09Z")

</div>

Hi @atanu ! 😄

I guess, it's simple. How can you login again when you are already login-ed into ODK Central? Tap on the "cookies" available on the top right, delete the stored cookies for [odkapi.capibuilder.org](http://odkapi.capibuilder.org), and then retry the request. It will return the new token. 😅

Great day ahead! 😄

---

<div class="post-metadata">

**Author:** ![atanu](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/atanu/32/18622_2.png) [@atanu](https://forum.getodk.org/u/atanu)\
**Post date:** [December 24, 2024, 7:56am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/3 "2024-12-24T07:56:31Z")

</div>

Well, not really - these are different clients - one is postman, ODK Central itself and CAPIBuilder react app. ODK Central is not currently preventing log in using multiple clients.

For example, the postman has no other session open (screenshot below). Consequently, it didn't store any cookie either (in postman client).

One possibility is ofcourse server is preventing access from postman client, but then the response shouldn't be cred error.

It was working a few days earlier, i know its wired behavior.

 ![CleanShot 2024-12-24 at 13.22.34@2x](https://getodk.b-cdn.net/uploads/default/original/3X/1/7/171ceeb502857e80bfa9cccec5ecde020f41db2c.png)

---

<div class="post-metadata">

**Author:** ![MinimalPotato](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/minimalpotato/32/30174_2.png) [@MinimalPotato](https://forum.getodk.org/u/MinimalPotato)\
**Post date:** [December 24, 2024, 9:24am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/4 "2024-12-24T09:24:25Z")

</div>

Here, a short demo, I feel like cookies itself is an issue here. Disabling cookie jar will keep on generating new tokens on each request. But, just out of curiosity, I still wonder, why re-run this request and generate new tokens (as each token's already valid for 24 hours if I am not wrong, unless terminated otherwise)? 🤔

![postman-odk-session-test](https://getodk.b-cdn.net/uploads/default/original/3X/3/e/3ee2a42b1defc8c3790f50f77105073077c15738.gif)

---

<div class="post-metadata">

**Author:** ![atanu](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/atanu/32/18622_2.png) [@atanu](https://forum.getodk.org/u/atanu)\
**Post date:** [December 24, 2024, 10:16am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/5 "2024-12-24T10:16:55Z")

</div>

Yes, cookie was set beforehand, must have logged in that window.

---

<div class="post-metadata">

**Author:** ![atanu](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/atanu/32/18622_2.png) [@atanu](https://forum.getodk.org/u/atanu)\
**Post date:** [December 24, 2024, 11:08am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/6 "2024-12-24T11:08:05Z")

</div>

If you don't mind, the reason i stumbled across the authentication issue to send a post request following documentation for entities and datasets:

**Creating Datasets**

> **[Dataset Management](https://docs.getodk.org/central-api-dataset-management/#creating-datasets)**
>
> Listing Datasets: GET /projects/{projectId}/datasets Request Parameters,, projectId, number Example: 16,,,, Response HTTP Status: 200 Content Type: application/json Example Schema, array,, name, st...

**Creating Entities**

> **[Entity Management](https://docs.getodk.org/central-api-entity-management/#creating-entities)**
>
> Entities Metadata: GET /projects/{projectId}/datasets/{name}/entities Request Parameters,, projectId, number Example: 16,, name, string Example: people,,,, Response HTTP Status: 200 Content Type: a...

The cookie is already set in the login tab in Postman. Now i am sending this request body (provided in the docs) for creating entities (also creating datasets):

```auto
{
  "entities": [
    {
      "uuid": "54a405a0-53ce-4748-9788-d23a30cc3afa",
      "label": "John Doe (22)",
      "data": {
        "firstName": "John",
        "age": "22"
      }
    },
    {
      "uuid": "0c3a7922-b611-42ca-a961-944e09fa9aa2",
      "label": "Amy Jane (38)",
      "data": {
        "firstName": "Amy",
        "age": "38"
      }
    }
  ]
 
}

```

Example post endpoint for creating entities is: [https://odkapi.capibuilder.org/v1/projects/13/datasets/people/entities](https://odkapi.capibuilder.org/v1/projects/13/datasets/people/entities)

In both cases, i am getting back this response

```auto
{
    "message": "Could not authenticate with the provided credentials.",
    "code": 401.2
}

```

screenshot below

 ![CleanShot 2024-12-24 at 16.36.59@2x](https://getodk.b-cdn.net/uploads/default/original/3X/d/a/dadbf3217028c85a66337e4e25f3a5221b5d0c76.png)

Any approach to posting datasets and entities?

---

<div class="post-metadata">

**Author:** ![chun\_hing\_yap](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/chun_hing_yap/32/13598_2.png) [@chun\_hing\_yap](https://forum.getodk.org/u/chun_hing_yap)\
**Post date:** [December 27, 2024, 12:35am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/7 "2024-12-27T00:35:05Z")

</div>

why not using basic auth at Authorization?

---

<div class="post-metadata">

**Author:** ![atanu](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/atanu/32/18622_2.png) [@atanu](https://forum.getodk.org/u/atanu)\
**Post date:** [December 27, 2024, 3:11am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/8 "2024-12-27T03:11:15Z")

</div>

I followed ODK auth method:

> Post login cred using login REST API endpoint.  
> ODK issues cookie and sets in the client (in this case Postman)  
> All other subsequent requests (such as GET/POST entities and datasets in this case) will use the cookie.

This test results in the same auth error for the older ODK Central version (v23.05) and the latest (v2024.3).

---

<div class="post-metadata">

**Author:** ![MinimalPotato](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/minimalpotato/32/30174_2.png) [@MinimalPotato](https://forum.getodk.org/u/MinimalPotato)\
**Post date:** [December 28, 2024, 10:09pm UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/9 "2024-12-28T22:09:23Z")

</div>

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!

1. Using token (the way we were already doing it).
2. 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:

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/2/1/21765c854c52c798253b51db36e4d3bf73b4beab.png)

You will be adding "base\_url" which will contain _**[https://odkapi.capibuilder.org](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**

1. 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:

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/c/7/c72ac1e48d12d4eac62e4e619eb520ac48bd828c.png)

1. Under request "Settings" tab, disable the cookie jar (the one I shared a short demo for).
2. And, under the "Scripts" tab, add the following .js script in "Post-Response" section:

```js
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:

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/3/1/3102b598ed5a2b88c9e892db30e29957e62850ee.png)

1. 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).
2. Create a new POST request, let's call it "AddEntity", it will look something like this:

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/a/a/aa8adc9105168f86144c6adc7a557fb640d24c80.png)

1. 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:

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/8/8/88f2ea08ae17cd2d2f04cb25186969a50591ec39.png)

1. 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**

1. When using this, we can simply delete the "Authenticate" POST request we created / saved during the first approach.
2. 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:

 ![image](https://getodk.b-cdn.net/uploads/default/original/3X/1/8/18a5ba9fe3c5ebddb7e70d47ba34ef97d96eaef1.png)

1. 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! 😄

---

<div class="post-metadata">

**Author:** ![atanu](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/atanu/32/18622_2.png) [@atanu](https://forum.getodk.org/u/atanu)\
**Post date:** [December 30, 2024, 5:50am UTC](https://forum.getodk.org/t/postman-for-login-post-api-returning-could-not-authenticate/51687/10 "2024-12-30T05:50:35Z")

</div>

Appreciate @MinimalPotato for your detailed response and time spent in creating/ sharing the scripts. The flow is well-understood for accessing datasets/ entity and method to access.

yes, datasets/ entity cant be indepdently accessed (which i was kinda of expecting), it has to be accessed through regular form creation API.

Happy holidays to you to and new years greetings.
