Ask a Question

How to add custom headers at parent level

SOLVED
Bipedal029
Occasional Contributor

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

1 ACCEPTED SOLUTION

Accepted Solutions
Bill_In_Irvine
Contributor

Re: How to add custom headers at parent level

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

View solution in original post

6 REPLIES 6
richie
Community Hero

Re: How to add custom headers at parent level

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

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Bipedal029
Occasional Contributor

Re: How to add custom headers at parent level

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
Contributor

Re: How to add custom headers at parent level

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

View solution in original post

richie
Community Hero

Re: How to add custom headers at parent level

hi,

im obviously misunderstanding. what does 'parent' level in regards to the resource, request, method hierarchy available within soapui mean?

do you mean to set a header in a single location - almost say at project level so every request within a specific project includes a specific header?

@Bill_In_Irvine.....could this be tailored for other REST uri parms e.g. matrix, query and template parms? im not sure when id want to do this....but still im curious.....

cheers!
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Bill_In_Irvine
Contributor

Re: How to add custom headers at parent level

Hi Richie,

 

Some global level elements certainly can be set, but I do that in the settings/preferences dialogue.  Unfortunately they will apply to all projects.

 

One example is to use my own custom User-Agent header when I want to bypass the request and use my own curl from groovy. So in the preferences under HTTP for "User-Agent header: I type curl/7.27.0. Other preferences apply globally too. I suspect this is not what the OP wants if the OP has multiple projects. I have not found how to do that in a groovy script. I also turn off compression "Request Compression" - choose None. 

 

The request parameters - resources, can be done in groovy as well using perhaps json, and I've done that. I don't know about matrix or template parms as I don't use them.

 

I prefer proper nomenclature too: Global: Across all projects, Events or Project: Affects all test suites, Test Suite - affects all test cases, etc.

 

regards

 

Bill

 

 

Bipedal029
Occasional Contributor

Re: How to add custom headers at parent level

What I mean is to set the header so that all requests for that particular project have it.  Say you import a SWAGGER file and it has all of the methods listed in a project.  Set the header in the tree of the project at the top so that all of the test suites you create from it have the header set.  The only way I could see to do it was to create a test suite with each of the individual methods and add them manually to each in the ReadyAPI tab for functional testing.

 

I think the answer is above in coding it and I will give it a shot, sorry for not being more clear.

cancel
Showing results for 
Search instead for 
Did you mean: