2

There's tons of documentation on Internet how to send a file stream in HTTP POST request in other languages, but not in Scalatra.

To the topic: I'd like to send an image as byte array or as a file stream (sorry for sloppy terminology, I'm an absolute newbie) via Scalatra post(). I already have backend Java functions that take byte array, convert it back to .jpg image and store it on the server. What I'm unclear on is the exact syntax how to do this in Scalatra.

That's what my post request looks like:

    val imageInBytes = ... //obtain image in bytes
    post("/images", ("image" -> imageInBytes))

However, Eclipse says that overloaded method post cannot be applied to (String, (String, Array[Byte]))

On the server side:

    post("/images"){
           contentType = "image/jpeg"  //for displaying the image
           val imInBytes = params("image")  //obtain data from request body
           //do something with it.
     }

Any help would be greatly appreciated!

J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84

1 Answers1

0

you would need to base64 url encode the image bytes then you can get it from the params bag.

You can also post a raw image to an endpoint and then read the inputStream.

Casual Jim
  • 1,179
  • 7
  • 13