简体   繁体   中英

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM