Search code examples
scalahttp-requestscalatra

Sending HTTP request using Scalatra & Scala


I have a Scalatra web application, when a user sends a get request to the server, ie. www.example.com/getsomething it routes to the following code:

class ExampleServlet extends ScalatraServlet {
   get("/getsomething") { 
    //code here
   }
}

In the body of the get I would like to send out another get request to another server and capture the JSON that is sent back eg:

var JSONback = GetRequest("www.SomeOtherApi.com/getsomeJSON")

How would I achieve something like this?

Thanks


Solution

  • For basic needs you could use scala.io.Source

    scala.io.Source.fromURL("http://www.google.de","latin1").mkString
    res10: String =
    <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage"     ="de"><head><meta content="text/html; charset=UTF-8"
    

    For more sophisticated use cases you can use any http-client-library like: