JSONError: "undefined" is not valid JSON

I am trying to take a couple of strings hash them to form a secret and use that as a source for a credential.

I am using Postman agent on my desktop and it reads “connected” and “listening on port 10533”. Despite this, I keep getting a “403 forbidden request origin is not valid” error even though I have my api key configured with my Postman token.

The big problem is a “JSONError: “undefined” is not valid JSON” error which I cannot solve.

Any assistance would be gratefully received. I have the auth set to “inherit auth from parents” and my variables (strings) are all included in the environment variables section. thanks….

Scripts

const secret = pm.environment.get("user_secret");
const dln = pm.environment.get("dln");
const spBase = pm.environment.get("sp_base");
const crypto = pm.require('npm:[email protected]')

if (!secret || !dln || !spBase) {
    throw new Error("Missing env vars: user_secret, dln, sp_base");
}

const sha = crypto.SHA256(crypto.enc.Utf8.parse(secret));
const secretHashHex = "0x" + sha.toString(crypto.enc.Hex);
pm.environment.set("secret_hash", secretHashHex);

const url = spBase + "/cert/issue";
const body = { dln: dln, secret_hash: secretHashHex, audience: "SP:DEMO" };

pm.sendRequest({
    url,
    method: "POST",
    header: [{ key: "Content-Type", value: "application/json" }],
    body: { mode: "raw", raw: JSON.stringify(body) }
}, async function (err, resp) {
    try {
        if (err) { throw err; }
        const data = await resp.json();
        pm.environment.set("cert_json", JSON.stringify(data.cert));
        pm.environment.set("signature", data.signature);
        pm.environment.set("sp_signer", data.signer);
        pm.environment.set("cert_userId", data.cert.userId);
        pm.environment.set("cert_dln", data.cert.dln);
        pm.environment.set("cert_exp", data.cert.exp.toString());
    }
    catch (e) {
        console.error("An error occurred:", err);
    }
});

post response

const spBase = pm.environment.get("sp_base");
const certJson = pm.environment.get("cert_json");
const signature = pm.environment.get("signature");
const spSigner = pm.environment.get("sp_signer");

pm.test("Cert and signature exist", function () {
  pm.expect(certJson).to.be.a("string");
  pm.expect(signature).to.match(/^0x[0-9a-fA-F]{130}$/);
});

const verifyReq = {
  url: spBase + "/cert/verify",
  method: "POST",
  header: [{ key: "Content-Type", value: "application/json" }],
  body: { mode: "raw", raw: JSON.stringify({ cert: JSON.parse(certJson), signature, expectedSigner: spSigner }) }
};

pm.sendRequest(verifyReq, function (err, resp) {
  pm.test("Verify endpoint responded", function () {
    pm.expect(err).to.be.null;
    pm.expect(resp).to.have.property("code", 200);
  });
  const data = resp.json();
  pm.test("Signature is valid", function () {
    pm.expect(data.ok).to.eql(true);
  });
});

pm.test("Response status code is 403", function () {
  pm.expect(pm.response).to.have.property("status", 403);
});

pm.test("Response time is less than 200ms", function () {
  pm.expect(pm.response.responseTime).to.be.below(200);
});


Hey! Just checking—where exactly are you seeing the JSON error? Is it happening in the pre-request script or after the response comes back?

One thing that might help is breaking your code into smaller parts and testing each bit separately. That way, it’s easier to spot where things go wrong.

Also, I noticed there aren’t any console.log statements in your code. Adding some logs for your variables could really help with debugging.

My first guess is that this line might be causing the issue:

const data = await resp.json();

It looks like that’s the only line in your pre-request script that’s trying to parse JSON, so it’s a good place to start.

Try logging the data variable right after that line to see what it contains. And just a heads-up—using the name data for a variable might cause issues, since it used to be reserved for data files in some environments.