How to generate a cookie and send it in a request in Jmeter

强颜欢笑 提交于 2020-01-14 13:49:08

问题


I am fairly new to Jmeter and hence having trouble figuring out the following: I am testing a web service that needs a valid cookie to be sent in header. I have an endpoint url against which the userid and password validates. How do I validate the credentials against the url and extract the cookie for the user and send it in header for the request in Jmeter?


回答1:


JMeter provides HTTP Cookie Manager which automatically handles cookies so in the majority of cases you don't need to do anything apart from adding the HTTP Cookie Manager to your Test Plan

However in some cases, i.e. in some CSRF implementations you need to add a request header holding previous response specific cookie value. In that case you should be acting like:

  1. Add the next line to user.properties file (lives in JMeter's "bin" folder

    CookieManager.save.cookies=true
    
  2. Restart JMeter to pick the property up. The above setting "tells" JMeter to store cookie values as JMeter Variables prefixed by COOKIE_. So for example if you have cookie with the name of foo you will be able to access its value as ${COOKIE_foo}
  3. Add HTTP Header Manager and set it up to send the desired header using ${COOKIE_foo} as a value (replace foo with your actual cookie name)

More detailed information: Using the HTTP Cookie Manager in JMeter



来源:https://stackoverflow.com/questions/40010164/how-to-generate-a-cookie-and-send-it-in-a-request-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!