I wrote this code to use the http4s client library to make a POST call to a REST web service
val client = SimpleHttp1Client()
val form = UrlForm("username" -> userName, "password" -> password)
val uri = Uri.fromString(url).valueOr(throw _)
val list = List(`Content-Type`(MediaType.`application/json`), Accept(MediaType.`application/json`))
val req = POST(uri, form).map(_.putHeaders(list :_*))
try {
val result = client.expect[String](req).unsafePerformSync
Some(result)
} catch {
case e : Throwable => println(e.getMessage); None
}
Right now, the code gets a 400 Bad Request error and I have no idea about why. I am not able to print the entire body of the error in the code above.
when I make the same REST call thru POSTMAN I can see 400 Bad request error body
{
"errors": [
"password: field is required",
"username: field is required"
]
}
How can I print the entire error body in my code?
Also, in postman when I set Content-Type and Accept headers the post call succeeds. In the code above, I am setting the same 2 headers and the same json body but still this code gets 400 error.
val list = List[Header](Header("Accept", "application/json"), Header("Content-Type", "application/json"))
val req = POST(uri, form).map(_.replaceAllHeaders(list :_*))
I had a conversation with the http4s team on gitter and I found the answer. since that conversation is not indexed by google I am writing the answer here
val output : Either[String, Foo] = client.fetch(request) {
case Successful(resp) => resp.as[Foo].map(Right(_))
case resp => resp.as[String].map(Left(_))
}
This if there is an error. we get the entire body of the error with this.