Forum Discussion

divman's avatar
divman
Contributor
10 years ago
Solved

Resolved:Get HTTP Response Code

Hi,

Sorry if this is repeated question, but i couldn't get the exact answer what i need.

I send a REST service and get the response back. In JSON tab, i get the response what i need to get and am validating that with my expectation. In Raw tab, i get the response like below.

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Date: Wed, 14 Jan 2015 15:33:32 GMT
Pragma: no-cache
Content-Length: 122
Content-Type: application/json
Expires: 0
Accept: application/json
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=2bnSJ2MMvL2wKNlQ6y3tZQy9vpv6Dv0Bh1Y6YprZvmWh76T5KV7K!-1868580631; path=/; HttpOnly
Set-Cookie: _WL_AUTHCOOKIE_JSESSIONID=TPX8bnRvzHF7C4U6e18T; path=/; secure; HttpOnly
X-Version-Served: 1
X-Powered-By: Servlet/2.5 JSP/2.1
X-Frame-Options: DENY

{"userReferenceId":"1zdYKGMazTOwKmgZIKbHz1Str3Pr9gHCUwqHdoEs5bM=","accessRoleId":79,"accessRoleAssignmentStatus":"ACTIVE"}


Here i need to get the first line out as "HTTP/1.1 200 OK" or the HTTP Error Code as "200 OK". Is there any way yo get that out?
  • Hope i got it.

    As i mentioned earlier, use script assertion.

    headers["#status#"] returns List. Looks you are asserting with string.
    So, you may use value coming from excel file for variable 'excelExpectedValue'

    def headers =  messageExchange.getResponseHeaders()
    log.info headers["#status#"]
    assert ['HTTP/1.1 200 OK'] == headers["#status#"]
    //log.info messageExchange.getResponseContent()
    headers["#status#"] instanceof List
    def actualStatus = headers["#status#"].get(0)
    def excelExpectedValue = '200 OK'
    def expectedStatus = 'HTTP/1.1 '+excelExpectedValue
    assert expectedStatus == actualStatus

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Have script assertion, and copy below lines

    def headers =  messageExchange.getResponseHeaders()
    log.info headers["#status#"]
    assert ['HTTP/1.1 200 OK'] == headers["#status#"]
  • Thank you so much Rao.

    I just found out the same stuff like below.
    req2 = testRunner.testCase.testSteps['Request 1'].testRequest.response.getResponseHeaders()
    log.info req2["#status#"]


    And i am trying to find the on how to get the output like this "200 OK", "400 Bad Request". I mean the "<Code> <Message>" format. Is this you can help me? Sorry if this is very basic question.
  • nmrao's avatar
    nmrao
    Champion Level 3
    Would you like to assert on the response? Did not get what you mean, anything further to do with output? Please clarify
    i am trying to find the on how to get the output
  • Yes, i need to assert that response. For example, I am getting the value "201 Created" from my excel source as a value for a scenario. Once the scenario has run, i need to validate or assert this value by talking the value from actual response after my test has run.

    Like this...

    assert "201 Created" (from the cell value of excel source) == "201 Created" (from the actual response after the test has run).

    But the response is giving my like this "[HTTP/1.1 201 Created]". Here i am trying to get only the "201 Created".

    Please let me know if i am not clear.
  • nmrao's avatar
    nmrao
    Champion Level 3
    Hope i got it.

    As i mentioned earlier, use script assertion.

    headers["#status#"] returns List. Looks you are asserting with string.
    So, you may use value coming from excel file for variable 'excelExpectedValue'

    def headers =  messageExchange.getResponseHeaders()
    log.info headers["#status#"]
    assert ['HTTP/1.1 200 OK'] == headers["#status#"]
    //log.info messageExchange.getResponseContent()
    headers["#status#"] instanceof List
    def actualStatus = headers["#status#"].get(0)
    def excelExpectedValue = '200 OK'
    def expectedStatus = 'HTTP/1.1 '+excelExpectedValue
    assert expectedStatus == actualStatus