JMeter : How to set a Cookie using Beanshell

Here is a Beanshell script that will add a cookie to the cookies sent in every HTTP request.

import org.apache.jmeter.protocol.http.control.Cookie;

// Retrieve the cookie value from a JMeter variable called "myCookieValue"
String cookieValue = vars.get("myCookieValue");

// Create a new Cookie and add it to the cookie manager
Cookie cookie = new Cookie("cookieName", cookieValue , "localhost", "/", false, -1);
ctx.getCurrentSampler().getCookieManager().add(cookie); 

log.info("Cookie added: "+cookie);

NB: Please remind to add a ´HTTP Cookie Manager´ to your thread group.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s