Skip to content

Instantly share code, notes, and snippets.

@fmasion
Created November 27, 2013 22:51
Show Gist options
  • Save fmasion/7684499 to your computer and use it in GitHub Desktop.
Save fmasion/7684499 to your computer and use it in GitHub Desktop.
Here a way to get around play issue #2015 This will enable you to add a body to WS GET and DELETE methods It uses an Implicit value class to extend WSRequestHolder The only "trick" is to use the same package name to overcome the private[Play] limitation Enjoy
// KEEP CARE TO LET THIS IN THIS PACKAGE
// this enables you to acces and extend private[Play] Objects
package play.api.libs.ws
import scala.concurrent.Future
import java.io.File
import play.api.http.{ Writeable, ContentTypeOf }
import play.api.libs.ws.WS._
import play.api.libs.iteratee.Iteratee
// Implicits are used to extend WSRequestHolder to enable body to be passed with GET and DELETE Methods
// Won't be needed after issue #2015 will be fixed
object Implicits {
implicit class WSRequestHolderOps(val rh: WSRequestHolder) extends AnyVal {
/**
* Perform a GET on the request asynchronously.
* @param body supplied won't be chunked
*/
def get[T](body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Response] = rh.prepare("GET", body).execute
/**
* Perform a GET on the request asynchronously.
* @param body supplied won't be chunked
*/
def get(body: File): Future[Response] = rh.prepare("GET", body).execute
/**
* performs a get with supplied body
* @param body won't be chunked
* @param consumer that's handling the response
*/
def get[A, T](body: T, consumer: ResponseHeaders => Iteratee[Array[Byte], A])(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Iteratee[Array[Byte], A]] =
rh.prepare("GET", body).executeStream(consumer)
/**
* Perform a DELETE on the request asynchronously.
* @param body supplied won't be chunked
*/
def delete[T](body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Response] = rh.prepare("DELETE", body).execute
/**
* Perform a DELETE on the request asynchronously.
* @param body supplied won't be chunked
*/
def delete(body: File): Future[Response] = rh.prepare("DELETE", body).execute
/**
* performs a delete with supplied body
* @param body won't be chunked
* @param consumer that's handling the response
*/
def delete[A, T](body: T, consumer: ResponseHeaders => Iteratee[Array[Byte], A])(implicit wrt: Writeable[T], ct: ContentTypeOf[T]): Future[Iteratee[Array[Byte], A]] =
rh.prepare("DELETE", body).executeStream(consumer)
}
}
....
import play.api.libs.ws.WS
// Add this to the regular WS imports
import play.api.libs.ws.Implicits._
...
// Now you can you the magicaly added Methods on WS object
WS.url(url).get(body)
WS.url(url).get(file)
WS.url(url).get(body, (_ => MyIteratee))
WS.url(url).delete(body)
WS.url(url).delete(file)
WS.url(url).delete(body, (_ => MyIteratee))
@scalaster
Copy link

Hi
I was able to post a body with the delete request using the withBody() method

wsClient.url(url)
      .withBody(body)
      .delete()
      .map (response => if (response.status == Status.NO_CONTENT) true else false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment