Forum Discussion

mwalter's avatar
mwalter
Occasional Contributor
8 years ago
Solved

How to reference an Environment property from the custom headers of a request

I need some help with my usage or syntax reading an Environment property.  I have a custom header property that I need to add to every request.  In essence it defines the back-end that goes with each of my environments, so it makes the most sense to instead have it as a custom property for each environment I have setup in the Environments section.  This part makes sense, but when I try to reference that Env property from the header it does not seem to be picking it up.  

 

Here is what I'm trying to add in a request header called FirmId:

${#Env#FirmId}

I'm thinking this 'should' read the FirmId property assigned to the environment that I have selected.  So far no luck.

 

Thanks,

Mike

  • Are you talking about using the custom properties that are displayed in the Environment dialog box? If so these are project custom properties.

     

    While I have never referenced them for the exact scenario you describe, via Groovy script you would reference them by with the following property expansion syntax:

     

    ${#Project#FirmId}

     

    Might be worth a try...


  • mwalter wrote:

    Hmmm....this is rather interesting.   I ran your example and saw the same issue as you.  Then I re-ran mine, and it is also now showing the same problem where it previously worked.


    OK I figured out how to make this work. The idea is to perform the property expansion using the context variable because that's what is available within the scope of the script when RequestFilter is used.

     

    def key = context.expand('${#Project#az_key}')

     

    Then i can use key as a variable to set the header i need:

     

    headers.put("Authorization", key)

     

    Hope this helps others!

  • dg3781,

    Yes, that actually does help and works well.  So thank you for that.   It still baffles me why the original solution stopped working.  My initial thought was that something changed with the 1.9.0 version because I did upgrade in between these 2 attempts but whatever, after I applied your change my script started working again.

12 Replies

  • Radford's avatar
    Radford
    Super Contributor

    Are you talking about using the custom properties that are displayed in the Environment dialog box? If so these are project custom properties.

     

    While I have never referenced them for the exact scenario you describe, via Groovy script you would reference them by with the following property expansion syntax:

     

    ${#Project#FirmId}

     

    Might be worth a try...

    • mwalter's avatar
      mwalter
      Occasional Contributor

      Thanks.  This was the piece of information I was missing and it works perfectly now.

    • dg3781's avatar
      dg3781
      Occasional Contributor

      edit: duplicate post. removing.

    • dg3781's avatar
      dg3781
      Occasional Contributor

      I am trying this too, but i keep getting a groovy error:

       

      Fri Oct 28 14:06:42 EDT 2016:ERROR:An error occurred [startup failed:
      Script77.groovy: 7: unexpected char: '#' @ line 7, column 12.
      log.info ${#Project#az_key}
      ^
      org.codehaus.groovy.syntax.SyntaxException: unexpected char: '#' @ line 7, column 12.
      at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:138)
      ...(truncated)


      Caused by: Script77.groovy:7:12: unexpected char: '#'
      at org.codehaus.groovy.antlr.parser.GroovyLexer.nextToken(GroovyLexer.java:719)

      ...(truncated)

       

      I have setup a custom project property using Environments window, called az_key. I need to send this az_key with each request as a header, so i'm using Events, with RequestFilter.filterRequest to add into headers (taken from the scripts documentation). While setting this key, the value should come from the custom project property. So, this is what i'm trying to do:

       

      def headers = request.requestHeaders
      headers.put( "Subscription-Key", ${#Project#az_key} )
      request.requestHeaders = headers

       

      However, groovy does not evaluate this. I tried to trace it by logging this expansion, but i get the error posted above:

       

      log.info ${#Project#az_key}

       

      Any ideas what i'm doing wrong, and how to get the value of this custom property?

      • mwalter's avatar
        mwalter
        Occasional Contributor

        Hi dg3781,

        Here's what worked for me.  Looking at you code vs what I did your change might be as simple as quotes around the environment property.  See if this works for you.

         

        def headers = request.requestHeaders
        headers.remove("FirmId")
        headers.put( "FirmId", "${#Project#FirmId}" )
        request.requestHeaders = headers

    • dg3781's avatar
      dg3781
      Occasional Contributor

      This page is not helpful at all. It doesn't explain how to access or use these custom properties. Please provide more context and not just a link to a page with barebones information.