Doing HTTP request in Scala

前端 未结 9 740
死守一世寂寞
死守一世寂寞 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: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")
    

提交回复
热议问题