Day 25: Getting [object Object] for payload

My Fellow Postmanauts,

I must be fried. I only have 3 days left to complete the last 5 challenges so this one is slipping away from me.

I cannot for the life of me get the {{payload}] env variable to update to match the hexPayload set in the pre-script. Here is my pre-script code.

let color_var = pm.variables.replaceIn('{{$randomHexColor}}');
  let hex_var = color_var.slice(1);


let hexPayload = pm.sendRequest("http://www.thecolorapi.com/id?hex=" + hex_var, function (err, response) {
    let JSONResponse = response.json();
    let bodyBuild = {
        hex: JSONResponse.hex.value,
        rgb:JSONResponse.rgb.value,
        name:JSONResponse.name.value
    }
    return JSON.stringify(bodyBuild);});

pm.environment.set("payload",hexPayload);```

Hi @terry-brooks

Try this;

const color_var = pm.variables.replaceIn('{{$randomHexColor}}');

pm.sendRequest(`http://www.thecolorapi.com/id?hex=${color_var.slice(1)}`, (err, response) => {
    if (err) 
    { 
        throw err; 
    }
    else 
    {
        let data = response.json();
        const bodyBuild = {
            hex: color_var,
            rgb: data.rgb.value,
            name: data.name.value
        };
        pm.collectionVariables.set("payload", JSON.stringify(bodyBuild));
        console.log(bodyBuild);
    }
});

The [object Object] for payload I believe is how/where you were using stringify.

There is some useful info on this here;

1 Like