Giter Site home page Giter Site logo

scalahttpclient's Introduction

How to build

Just run ant, then copy and paste jar file to your classpath. Ivy is needed to resolve dependencies.

Dependencies

All libraries are used to execute unit test.
  • scalatest
  • junit
  • commons-codec

Created jar is executable without any dependencies.

How to use

Just import com.shc.http:

import com.shc.http._

Here is a simple GET request. This returns request.Response instance obj:

val url = "http://example.com"
var resp = Request.get(url)

If you want to add query strings, just pass key-value map to the method:

resp = Request.get(url, Map("name" -> "foofoo"))

How to retrieve response contents:

if(resp != None){
  val url = resp.get.url // java.net.URL
  val statusCode = resp.get.statusCode // String
  val headers = resp.get.headers // Map[String, List[String]]
  val message = resp.get.message // String
  val cookies = resp.get.cookies // List[java.net.HttpCookie]
}

Uploading files:

val files = Map("key" -> "/where/the/file/exist")
resp = Request.post(url, files = files)
resp = Request.post(url, Map("key" -> "value"), files)

Creating authentication credential. This support basic & digest auth:

if(resp != None & resp.get.statusCode == 401){
  resp = Request.get(url, auth = resp.auth("user", "pass"))
  println(resp.get.statusCode == 200)
}

Reusing cookieManager:

val cookies = resp.cookies.foreach(println)
val cookieManager = resp.cookieManager
resp = Request.get(url, cookie = cookieManager)

Storing contents:

val path = "/path/to/somewhere"
val bool = resp.store(path)
println(bool)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.