ICodeForCaffeine
ICodeForCaffeine

Reputation: 195

Issue retrieving Token with application/x-www-form-urlencoded using Bruno Script

I'm using Bruno and encountering an issue with a script to fetch a token from an endpoint.

When the Content-Type is application/json, everything works as expected, and I can retrieve and set the token successfully. Here’s the working script for application/json:

const axios = require("axios");
const https = require("https");
const httpsAgent = new https.Agent({
    rejectUnauthorized: false
});
var path = "https://private-api-dev.pre/internal/jwt/generate";
var headers = {
    "Content-Type": "application/json",
    "Cache-Control": "no-cache",
    "client-id": "123-456-789",
    "client-secret": "123456789"
};
var body = JSON.stringify({
    subject: "est",
    audience: "customers",
    miscInformation: "testing"
});
const auth = await axios.post(path, body, {
    headers,
    httpsAgent
})
    .then((response) => {
        bru.setEnvVar("jwt_token_dev", response.data.jwt);
        console.log("Fetched JWT Token:", response.data.jwt);
    })
    .catch((error) => {
        console.error("Error fetching token:", error.message);
    });

However, when the endpoint requires the Content-Type to be application/x-www-form-urlencoded, the script doesn’t work, and the token isn’t updated. I've adjusted the body and headers like this:

const qs = require('qs');
var headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Cache-Control": "no-cache",
    "client-id": "123-456-789",
    "client-secret": "123456789"
};
var body = qs.stringify({
    subject: "est",
    audience: "customers",
    miscInformation: "testing"
});
const auth = await axios.post(path, body, {
    headers,
    httpsAgent
})
    .then((response) => {
        bru.setEnvVar("jwt_token_dev", response.data.jwt);
        console.log("Fetched JWT Token:", response.data.jwt);
    })
    .catch((error) => {
        console.error("Error fetching token:", error.message);
    });

Despite the changes, the jwt_token_dev variable isn’t updated. I’ve confirmed that when calling the endpoint directly, everything works fine, and I receive the token. I also tried:

const body = new URLSearchParams({
    subject: "est",
    audience: "customers",
    miscInformation: "testing",
}).toString();

Also fetch:

fetch(path, {
    method: "POST",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "x-ibm-client-id": "client-id",
        "x-ibm-client-secret": "client-secret",
    },
    body: new URLSearchParams({
        subject: "est",
        audience: "customers",
        miscInformation: "testing",
    }),
    agent: httpsAgent,
})

Is there something I’m missing when using application/x-www-form-urlencoded in this script?

Upvotes: 0

Views: 93

Answers (0)

Related Questions