Skip to content

Commit

Permalink
setContentType sets body encoding too
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Dec 17, 2013
1 parent 744a0cc commit 2ae0e0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 6 additions & 8 deletions core/src/main/scala/requests.scala
Expand Up @@ -84,10 +84,6 @@ trait ParamVerbs extends RequestVerbs {
subject.setMethod(method)
else subject
}
private def defaultBodyEncoding(charset: String): Req = {
if (Option(subject.toRequest.getBodyEncoding).isEmpty) subject.setBodyEncoding(charset)
else subject
}
private def defaultContentType(mediaType: String, charset: String): Req = {
if (!subject.toRequest.getHeaders.containsKey("Content-Type"))
subject.setContentType(mediaType, charset)
Expand All @@ -104,11 +100,10 @@ trait ParamVerbs extends RequestVerbs {
}
/** Set request body to a given string,
* - set method to POST if currently GET,
* - set HTTP Content-Type to "text/plain; charset=UTF-8" if unspecified,
* - and set body encoding to UTF-8 if unspecified. */
* - set HTTP Content-Type to "text/plain; charset=UTF-8" if unspecified. */
def << (body: String) = {
defaultMethod("POST").defaultContentType("text/plain", "UTF-8").
defaultBodyEncoding("UTF-8").setBody(body)
setBody(body)
}
/** Set a file as the request body and set method to PUT if it's
* currently GET. */
Expand Down Expand Up @@ -172,7 +167,10 @@ trait RequestBuilderVerbs extends RequestVerbs {
def setBodyEncoding(charset: String) =
subject.underlying { _.setBodyEncoding(charset) }
def setContentType(mediaType: String, charset: String) =
subject.underlying { _.setHeader("Content-Type", mediaType + "; charset=" + charset) }
subject.underlying {
_.setHeader("Content-Type", mediaType + "; charset=" + charset).
setBodyEncoding(charset)
}
def setHeader(name: String, value: String) =
subject.underlying { _.setHeader(name, value) }
def setHeaders(headers: Map[String, Seq[String]]) =
Expand Down
6 changes: 4 additions & 2 deletions docs/03/a.markdown
Expand Up @@ -45,10 +45,12 @@ parameters to the body at once:
def myPostWithParams = myRequest << Map("key" -> "value")
```

You can also POST an arbitrary string:
You can also POST an arbitrary string. Be sure to set MIME media type
and character encoding:

```scala
def myPostWithBody = myRequest << """{"key": "value"}"""
def myRequestAsJson = myRequest.setContentType("application/json", "UTF-8")
def myPostWithBody = myRequestAsJson << """{"key": "value"}"""
```

### Query parameters
Expand Down

0 comments on commit 2ae0e0a

Please sign in to comment.