Doing HTTP request in Scala

前端 未结 9 736
死守一世寂寞
死守一世寂寞 2020-12-12 14:31

I am trying to issue a simple POST request to a webservice which returns some XML in Scala.

It seems that Dispatch is the standard library used for this task, but I

相关标签:
9条回答
  • 2020-12-12 14:45

    You could use spray-client. The documentation is lacking (it took me some digging to find out how to make GET requests with query parameters) but it's a great option if you are already using spray. And the documentation is better than dispatch.

    We're using it at AI2 over dispatch because the operators are less symbolic and we're already using spray/actors.

    import spray.client.pipelining._
    
    val url = "http://youruri.com/yo"
    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    
    // Post with header and parameters
    val responseFuture1: Future[String] = pipeline(Post(Uri(url) withParams ("param" -> paramValue), yourPostData) map (_.entity.asString)
    
    // Post with header
    val responseFuture2: Future[String] = pipeline(Post(url, yourPostData)) map (_.entity.asString)
    
    0 讨论(0)
  • Why not use Apache HttpComponents ? Here's the application FAQ, which covers a wide range of scenarios.

    0 讨论(0)
  • 2020-12-12 14:49

    I had to do the same to test one end point (in Integration test). So following is the code to fetch response from GET request in Scala. I am making use of scala.io.Source to read from endpoint and ObjectMapper for json to object conversion.

    private def buildStockMasterUrl(url:String, stockStatus:Option[String]) = {
          stockStatus match  {
            case Some(stockStatus) => s"$url?stockStatus=${stockStatus}"
            case _ => url
        }
      }
    
        private def fetchBooksMasterData(stockStatus:Option[String]):  util.ArrayList[BooksMasterData] = {
        val url: String = buildBooksMasterUrl("http://localhost:8090/books/rest/catalogue/booksMasterData",stockStatus)
        val booksMasterJson : String = scala.io.Source.fromURL(url).mkString
        val mapper = new ObjectMapper()
        apper.readValue(booksMasterJson,classOf[util.ArrayList[BooksMasterData]])
    }
    
    case class BooksMasterData(id:String,description: String,category: String)
    

    And here is my test method for the same

    test("validate booksMasterData resource") {
        val booksMasterData = fetchBooksMasterData(Option(null))
        booksMasterData.size should be (740)
      }
    
    0 讨论(0)
  • 2020-12-12 14:50

    Using my Requests-Scala library:

    // Mill
    ivy"com.lihaoyi::requests:0.1.8"
    // SBT
    "com.lihaoyi" %% "requests" % "0.1.8"
    

    This is as simple as

    val r = requests.get("https://api.github.com/users/lihaoyi")
    
    r.statusCode
    // 200
    
    r.headers("content-type")
    // Buffer("application/json; charset=utf-8")
    
    r.text
    // {"login":"lihaoyi","id":934140,"node_id":"MDQ6VXNlcjkzNDE0MA==",...
    
    val r = requests.post("http://httpbin.org/post", data = Map("key" -> "value"))
    
    val r = requests.put("http://httpbin.org/put", data = Map("key" -> "value"))
    
    val r = requests.delete("http://httpbin.org/delete")
    
    val r = requests.head("http://httpbin.org/head")
    
    val r = requests.options("http://httpbin.org/get")
    
    0 讨论(0)
  • 2020-12-12 14:51

    I'm using dispatch: http://dispatch.databinder.net/Dispatch.html

    They've just released a new version (0.9.0) with a complete new api that I really like. And it is async.

    Example from project page:

    import dispatch._
    val svc = url("http://api.hostip.info/country.php")
    val country = Http(svc OK as.String)
    
    for (c <- country)
      println(c)
    

    edit: This might help you https://github.com/dispatch/reboot/blob/master/core/src/main/scala/requests.scala

    0 讨论(0)
  • 2020-12-12 14:55

    If I can make a shameless plug, I have an API called Bee-Client which is simply a wrapper in Scala for Java's HttpUrlConnection.

    0 讨论(0)
提交回复
热议问题