Sending cookie as request header in SOAP UI request for rest web service

混江龙づ霸主 提交于 2019-12-28 13:46:29

问题


I am testing a Rest API using SOAP UI tool.

First, I hit another API which gave me the jSessionid and then in my actual request I added a parameter to the request header by the name "Cookie" and provide it the above extracted jsessionid value. Everything worked fine.

What I want is to somehow dynamically set the cookie/jessionid in request header without explicitly/manually doing it.

How can it be done?


回答1:


You can create a application session from the UI and use cookies that save the session. Get the session cookie details from the browser using the developer tools of the browser. The image shows where the cookies are available in the browser :

In soapUI create a header attribute with the same value you obtained from the browser and run the request. The request will use the same session id(JSESSIONID) that is saved in the cookie to run the request. The below images shows how to add the cookie value to the header.




回答2:


You can easily manipulate Cookies using Groovy scripting. In SoapUI, Cookies are kept in the Cookie store:

import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport
def myCookieStore = HttpClientSupport.getHttpClient().getCookieStore()

You can read all the Cookies in there:

def myCookies = myCookieStore.getCookies()
def interestingCookie
myCookies.each {
    if(it.name == "JSESSIONID")
        interestingCookie = it
}

To create a new Cookie in another request:

import org.apache.http.impl.Cookie.BasicClientCookie
def myNewCookie = new BasicClientCookie("cookie_name", "cookie_value")
myNewCookie.version = 1
myNewCookie.domain = "qa.test"
myCookieStore.addCookie(myNewCookie)

I have some additional information in an older blog post here.



来源:https://stackoverflow.com/questions/28022721/sending-cookie-as-request-header-in-soap-ui-request-for-rest-web-service

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