我正在尝试发送带有有效负载(xml)的POST请求。这是我的代码示例:
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
问题:如果我通过SOAP UI(Rest)发送了相同的XML,则得到预期的XML响应。通过此代码,我在日志中看到以下内容:
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]
日志下面是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 - >> " "
问题1:为什么我有以下几行(考虑到上面提到的代码返回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:在日志中重复复制行之后。为什么有效载荷会相应发送两次以记录日志:
DEBUG o.a.h.i.c.DefaultHttpClient- Attemp 2 to execute request //Why 2 request?
DEBUG o.a.h.i.c.DefaultClientConnection-发送请求: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
答案 0 :(得分:0)
Q1通过添加preemptive()来解决。在basic()之前,所以我没有401错误