Forum Discussion

Bipedal029's avatar
Bipedal029
Occasional Contributor
7 years ago
Solved

How to add custom headers at parent level

Hello,

 

I am trying to add custom headers at the parent level so that every request does not have to be individually tailored.  I see there may be some scripting options or property expansions but I can't find a good way to implement it and where to implement it.

 

Say I want to add custom header:   "x-id: value" to all requests.

 

How would I go about doing that?

 

Thank you

  • You can do this at the parent level (by that I am guessing so that all requests use the same headers is what you want) - by doing so in an event handler. 

     

    So that in the RequestFilter.filterRequest event handler if you want to add "Content-Type: application/x-www-form-urlencoded" as a header you can do it the following way:

     

    def headers = request.requestHeaders
    
    // I clear out all request content, but you might not want to do this:
    context.setProperty("requestContent", '')
    request.requestContent = ''
    
    // add the header
    headers.put( "Content-Type", "application/x-www-form-urlencoded") 
    
    request.requestHeaders = headers
    
    
    

    Now every request will have the "application/x-www-form-urlencoded" Content-Type.

     

    Bill

6 Replies

  • richie's avatar
    richie
    Community Hero

    Hi,

     

    Are you using ReadyAPI!?

     

    I may be misunderstanding the complexities of your situation but in ReadyAPI! it's quite straight forward.

     

    You can add headers at Resource, Method or Request level within the 'Projects' tab

     

    1.  Create your resource, method and request within the 'Projects' tab

    2.  Click on the 'Add Parameter' option at any of these levels

    3.  Click on the 'Name' column and insert x-id

    4.  Click in the 'Value' column specifying the relevant value - beit specific or you can tailor it with property expansion

    5.  In the 'Style' column, hit the dropdown and select 'HEADER' option

     

    and you're good to go!

     

    Then - if you go into the SoapUI tab and within your test case, add the relevant test step that corresponds to the request you've just added the header to and the header will be present

     

    Cheers,

     

    richie

    • Bipedal029's avatar
      Bipedal029
      Occasional Contributor

      Hello,

       

      I'm actually referring to the ability to add it at a parent level so that all requests created would have them with one addition.  I don't think your instructions cover that and I see other users looking for the same information.

      • Bill_In_Irvine's avatar
        Bill_In_Irvine
        Contributor

        You can do this at the parent level (by that I am guessing so that all requests use the same headers is what you want) - by doing so in an event handler. 

         

        So that in the RequestFilter.filterRequest event handler if you want to add "Content-Type: application/x-www-form-urlencoded" as a header you can do it the following way:

         

        def headers = request.requestHeaders
        
        // I clear out all request content, but you might not want to do this:
        context.setProperty("requestContent", '')
        request.requestContent = ''
        
        // add the header
        headers.put( "Content-Type", "application/x-www-form-urlencoded") 
        
        request.requestHeaders = headers
        
        
        

        Now every request will have the "application/x-www-form-urlencoded" Content-Type.

         

        Bill