Send a request in pre-request with the API Key in the header

Hi

I want to send a request from the pre-request Script but my endpoint uses API Key as headers to access, so I have next:

and the next code

function sendRequest(name){
   pm.sendRequest({
        url:  (pm.collectionVariables.get("baseUrl") + "/companies"),
        method: 'POST',
        headers: {
            'sn-api-key': pm.collectionVariables.get("snApiKeyInternal"),
            'sn-api-secret': pm.collectionVariables.get("snApiSecretExternal")
  },
        body: {
            mode: 'raw',
            raw:JSON.stringify(name)
        } 
    },(err,response) => {
        console.log(response);
    });
}

but Iā€™m getting a 401 error. Any idea?

Thanks in advance.

Hey @altimetry-physicis10 :wave:

Welcome to the Postman community! :postman:

I believe it should be header and not headers, that might be one the issues here.

You also seem to be mixing the internal API key and the external secret.

sendRequests are also logged in the console, so check the console as a troubleshooting step to ensure any variables being sent are being transposed correctly.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.