0

I'm trying to send a POST request with payload (xml). Here is my code example:

val xml = """
| <?xml version="1.0" encoding="UTF-8"?>
........
........
         """.stripMargin

RestAssured.baseURI = "https://xxxxxxxxx"
val request: RequestSpecification = given().port(1234).auth().basic("test", "test")
request
.header("Accept", ContentType.XML)
.contentType("application/xml")
.body(xml)

val response = request.post("/transformToXML2")
val statusCode = response.getStatusCode
Assert.assertEquals(statusCode, 200) //PASSED

Problem: If i send the same XML via SOAP UI (Rest) i got the response with XML expected. By this code i see the following in my log:

DEBUG o.a.h.i.c.DefaultClientConnection - Sending request: POST /transformToXML2 HTTP/1.1
DEBUG o.a.h.wire - >> "POST /transformToXML2 HTTP/1.1[\r][\n]
DEBUG o.a.h.wire - >> "Accept: application/xml[\r][\n]
....
DEBUG o.a.h.wire - >> "Content-Length: 17333[\r][\n]
.......
DEBUG o.a.h.wire - >> "Accept-Encoding:gzip,deflate[\r][\n]
DEBUG o.a.h.wire - >> [\r][\n]




DEBUG o.a.h.headers - >> "POST /transformToXML2 HTTP/1.1[\r][\n]
    DEBUG o.a.h.headers - >> "Accept: application/xml[\r][\n]
    ....
    DEBUG o.a.h.headers - >> "Content-Length: 17333[\r][\n]
    .......
    DEBUG o.a.h.headers - >> "Accept-Encoding:gzip,deflate[\r][\n]
    DEBUG o.a.h.headers - >> [\r][\n]

Below in logs is XML:

DEBUG o.a.h.wire - >> [\r][\n]
DEBUG o.a.h.wire - >> "<?xml version="1.0" encoding="UTF-8"?>"[\r][\n]
.....
DEBUG o.a.h.wire - >> " "

Q1: Why i have the following rows (taking into account above mentioned code returns 200)

 DEBUG o.a.h.wire - << "HTTP/1.1 401 Unauthorized"[\r][\n]
    DEBUG o.a.h.wire - << "Connection: keep-alive"[\r][\n]
    DEBUG o.a.h.wire - << "WWW-Authenticate: Basic realm="Realm name"[\r][\n]"
    DEBUG o.a.h.wire - << "Content-Length: 0"[\r][\n]
DEBUG o.a.h.i.c.DefaultClientConnection - Receiving request: HTTP/1.1 401 Authorized

Q2:Following that the rows are duplicated a bit in log. Why payload is sent twice accordingly to log:

DEBUG o.a.h.i.c.DefaultHttpClient- Attemp 2 to execute request //Why 2 request?

DEBUG o.a.h.i.c.DefaultClientConnection - Sending request: POST /transformToXML2 HTTP/1.1

DEBUG o.a.h.wire - >> "POST /transformToXML2 HTTP/1.1[\r][\n]
DEBUG o.a.h.wire - >> "Accept: application/xml[\r][\n]
DEBUG o.a.h.wire - >> "Content-Type: application/xml; charset=ISO-885901[\r][\n]
DEBUG o.a.h.wire - >> "Content-Length: 17333[\r][\n]
DEBUG o.a.h.wire - >> "Host: host+port [\r][\n]
DEBUG o.a.h.wire - >> "Connection: Keep-Alive[\r][\n]
DEBUG o.a.h.wire - >> "User-Agent: Apache-HttpClient/4.5.3..[\r][\n]
DEBUG o.a.h.wire - >> "Autorithation: Basic qwertyqweqwe[\r][\n]
DEBUG o.a.h.wire - >> "Accept-Encoding: gzip,deflate[\r][\n]
DEBUG o.a.h.i.c.DefaultClientConnection - Receiving request: HTTP/1.1 200 OK
DEBUG o.a.h.headers << HTTP/1.1 200 Ok
DEBUG o.a.h.headers << Content-Length: 107
DEBUG o.a.h.headers << Content-Type: application/xml
27P
  • 1,183
  • 16
  • 22
  • Q1 is resolved by adding preemptive(). before basic() so i do not have 401 error – 27P Feb 25 '19 at 15:35

1 Answers1

0

Q1 is resolved by adding preemptive(). before basic() so i do not have 401 error

27P
  • 1,183
  • 16
  • 22
  • For Q2 adding preemptive() should help as well. When doing non-preemptive authentication request client sends 2 requests (first without credentials, second with). With preemptive authentication credentials are sent with first request already – bhusak Feb 25 '19 at 15:54