[2.0] multipart/form-data in WS POST

2,668 views
Skip to first unread message

Mikael Ståldal

unread,
May 23, 2012, 5:28:21 AM5/23/12
to play-framework
How can I make a POST request to an external Web Service with a
multipart/form-data body?

Using play.api.lib.ws.WS from Scala.

Sadache Aldrobi

unread,
May 23, 2012, 12:32:00 PM5/23/12
to play-fr...@googlegroups.com
It is not possible with the current api.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--
www.sadekdrobi.com
ʎdoɹʇuǝ

Mikael Ståldal

unread,
May 24, 2012, 4:26:32 AM5/24/12
to play-framework
On 23 Maj, 18:32, Sadache Aldrobi <sadek.dr...@gmail.com> wrote:
> It is not possible with the current api.

I hope it will be added in some future version. I managed to do it by
using parts of Apache Http Client, but it resulted in quite messy
code:

Dependency in Build.scala: "org.apache.httpcomponents" % "httpclient"
% "4.2"

Code:

val boundary = "-----{}}AAA{{}-----"
val multipart = new HttpMultipart("form-data", boundary)
multipart.addBodyPart(new FormBodyPart("file", new
StringBody(myData)))
val buffer = new ByteArrayOutputStream()
multipart.writeTo(buffer)
val data = buffer.toByteArray
WS.url(myUrl).withHeaders((HeaderNames.CONTENT_TYPE, "multipart/
form-data; boundary="+boundary)).post(data)

James Roper

unread,
May 25, 2012, 3:58:03 PM5/25/12
to play-fr...@googlegroups.com
I'm pretty sure it is possible, definitely is with the Java API, it's just that you need to encode the body yourself (which for multipart/form-data is no simple task).


On Wednesday, 23 May 2012 18:32:00 UTC+2, Sadache wrote:
It is not possible with the current api.

On Wed, May 23, 2012 at 11:28 AM, Mikael Ståldal <mikael.staldal@appearnetworks.com> wrote:
How can I make a POST request to an external Web Service with a
multipart/form-data body?

Using play.api.lib.ws.WS from Scala.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-framework@googlegroups.com.
To unsubscribe from this group, send email to play-framework+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--
www.sadekdrobi.com
ʎdoɹʇuǝ

SJ

unread,
Sep 6, 2013, 6:12:40 PM9/6/13
to play-fr...@googlegroups.com
Hi.

Is this bug fixed yet ? I am trying to post a photo to facebook using WS play. 

Julien L.

unread,
Sep 7, 2013, 4:53:31 AM9/7/13
to play-fr...@googlegroups.com

Will Sargent

unread,
Sep 7, 2013, 10:06:27 PM9/7/13
to play-fr...@googlegroups.com
I have a pull request open, it needs some more work before it can be accepted.  You can use the underlying client directly using WS.client.

Will.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

nafg

unread,
Dec 5, 2014, 2:06:29 AM12/5/14
to play-fr...@googlegroups.com, will.s...@gmail.com
Where is the pull request?

Will Sargent

unread,
Dec 5, 2014, 1:00:22 PM12/5/14
to play-fr...@googlegroups.com, Will Sargent
It's out of date now: https://github.com/playframework/playframework/pull/1510

The way to do this is something like:

val asyncHttpClient = WS.client.underlying
val postBuilder = asyncHttpClient.preparePost("http://localhost/upload")
val builder = builder.addBodyPart(new FilePart("name", new File("file")))
asyncHttpClient.executeRequest(builder.build());

Will Sargent
Consultant, Professional Services
Typesafe, the company behind Play Framework, Akka and Scala

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages