简体   繁体   English

使用Scalatra和Scala发送HTTP请求

[英]Sending HTTP request using Scalatra & Scala

I have a Scalatra web application, when a user sends a get request to the server, ie. 当用户向服务器发送获取请求时,我有一个Scalatra Web应用程序。 www.example.com/getsomething it routes to the following code: www.example.com/getsomething会路由到以下代码:

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: 在获取的主体中,我想向另一个服务器发送另一个获取请求,并捕获发送回的JSON,例如:

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

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: 对于更复杂的用例,您可以使用任何http-client-library,例如:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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