ruimo
ruimo

Reputation: 343

How can I upload file from string in playframework?

PlayFramework docs shows that it is easy to upload a file.

https://www.playframework.com/documentation/2.5.x/ScalaWS

ws.url(url).post(Source(FilePart("hello", "hello.txt", Option("text/plain"), FileIO.fromFile(tmpFile)) :: DataPart("key", "value") :: List()))

But what if the file content is already in memory? Any alternative method for FileIO.fromFile, such as FileIO.fromString(jsontStr)?

val jsonStr = """{ foo: "Bar"} """
ws.url(url).post(Source(FilePart("hello", "hello.json", Option("application/json"), FileIO.fromString(jsonStr)) :: DataPart("key", "value") :: List()))

Upvotes: 0

Views: 255

Answers (1)

Nabil A.
Nabil A.

Reputation: 3400

All you need is a FilePart that has a Source[ByteString] as ref. Just use

Source.single(ByteString(jsonStr))

as the ref part.

Upvotes: 2

Related Questions