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?

  • RonSijm@programming.dev
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    9 months ago

    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.eeOP
      link
      fedilink
      arrow-up
      3
      ·
      9 months ago

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