-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathStandaloneWSResponse.scala
141 lines (124 loc) · 3.93 KB
/
StandaloneWSResponse.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/
package play.api.libs.ws
import org.apache.pekko.stream.scaladsl.Source
import org.apache.pekko.util.ByteString
/**
* A WS HTTP response.
*/
trait StandaloneWSResponse {
/**
* Returns the URI for this response, which can differ from the request one
* in case of redirection.
*/
def uri: java.net.URI
/**
* Returns the current headers for this response.
*/
def headers: Map[String, scala.collection.Seq[String]]
/**
* Get the value of the header with the specified name. If there are more than one values
* for this header, the first value is returned. If there are no values, than a None is
* returned.
*
* @param name the header name
* @return the header value
*/
def header(name: String): Option[String] = headerValues(name).headOption
/**
* Get all the values of header with the specified name. If there are no values for
* the header with the specified name, than an empty sequence is returned.
*
* @param name the header name.
* @return all the values for this header name.
*/
def headerValues(name: String): scala.collection.Seq[String] = headers.getOrElse(name, Seq.empty)
/**
* Get the underlying response object.
*/
def underlying[T]: T
/**
* The response status code.
*/
def status: Int
/**
* The response status message.
*/
def statusText: String
/**
* Get all the cookies.
*/
def cookies: scala.collection.Seq[WSCookie]
/**
* Get only one cookie, using the cookie name.
*/
def cookie(name: String): Option[WSCookie]
/**
* @return the content type.
*/
def contentType: String = header("Content-Type").getOrElse("application/octet-stream")
/**
* The response body as the given type. This renders as the given type.
* You must have a BodyReadable in implicit scope.
*
* The simplest use case is
*
* {{{
* import play.api.libs.ws.StandaloneWSResponse
* import play.api.libs.ws.DefaultBodyReadables._
*
* def responseBodyAsString(response: StandaloneWSResponse): String =
* response.body[String]
* }}}
*
* But you can also render as JSON
*
* {{{
* // not compilable: requires `play-ws-standalone-json` dependency
* import play.api.libs.json.JsValue
* import play.api.libs.ws.StandaloneWSResponse
*
* def responseBodyAsJson(response: StandaloneWSResponse): JsValue =
* response.body[JsValue]
* }}}
*
* or as binary:
*
* {{{
* import org.apache.pekko.util.ByteString
* import play.api.libs.ws.StandaloneWSResponse
* import play.api.libs.ws.DefaultBodyReadables._
*
* def responseBodyAsByteString(response: StandaloneWSResponse): ByteString =
* response.body[ByteString]
* }}}
*/
def body[T: BodyReadable]: T = {
val readable = implicitly[BodyReadable[T]]
readable.transform(this)
}
/**
* The response body decoded as String, using a simple algorithm to guess the encoding.
*
* This decodes the body to a string representation based on the following algorithm:
*
* 1. Look for a "charset" parameter on the Content-Type. If it exists, set `charset` to its value and go to step 3.
* 2. If the Content-Type is of type "text", set charset to "ISO-8859-1"; else set `charset` to "UTF-8".
* 3. Decode the raw bytes of the body using `charset`.
*
* Note that this does not take into account any special cases for specific content types. For example, for
* application/json, we do not support encoding autodetection and will trust the charset parameter if provided..
*
* @return the response body parsed as a String using the above algorithm.
*/
def body: String
/**
* @return The response body as ByteString.
*/
def bodyAsBytes: ByteString
/**
* @return the response as a source of bytes
*/
def bodyAsSource: Source[ByteString, _]
}