this post was submitted on 15 Feb 2024
18 points (100.0% liked)

Learn Programming

1625 readers
2 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
 

I get postman exports from students which I use to check their work. The authorisation of those requests now often contain hardcoded jwt tokens that are invalid by the time I get to checking them and I have to change every individual request with a global variable.

I do instruct my students to use variables, but there's always a couple who just don't, but that's a whole different issue.

Right now I'm using a regex find and replace to remove the Request authorization header in the json export file (which than defaults to 'inherit from parent'). This sort of works, but isn't ideal.

Do any of you know if postman offers an easier solution for this?

top 2 comments
sorted by: hot top controversial new old
[โ€“] RonSijm@programming.dev 4 points 9 months ago* (last edited 9 months ago) (1 children)

You can do it though the API.

Create a get request to https://api.getpostman.com/collections/' + collection_id, and then in the Test tab where you usually run scripts from, you can use something like this:

var postman_api_key = pm.environment.get("postman_api_key");

var response = pm.response.json();
var collection = response.collection;
var collection_id = request.url.split('/')[4];

function processItem(item){

    for (let i = 0; i < item.length; i++) {

        if(item[i].request){
            if(item[i].request.auth){
                var t = {};
                t.type = 'inherit';
                item[i].request.auth = t;
            }
        }

        if(item[i].item){
            item[i].item = processItem(item[i].item);
        }        

    } 
    return item;
}


collection.item = processItem(collection.item);

var update_collection = {};
update_collection['collection'] = collection;

const postRequest = {
  url: 'https://api.getpostman.com/collections/' + collection_id,
  method: 'PUT',
  header: 'x-api-key:' + postman_api_key,
  body: {
    mode: 'raw',
    raw: JSON.stringify(update_collection)
  }
};

pm.sendRequest(postRequest, (error, response) => {
  console.log(error ? error : response.json());
});

This probably isn't doing 100% exactly what you want, but it's probably close enough that you get the idea, and can modify it yourself.

Then just execute the dummy request

[โ€“] abbadon420@lemm.ee 2 points 9 months ago

I can't get it to works as of yet, but this looks great. I'll give it another try tomorrow.