Giter Site home page Giter Site logo

scalaj-http's Introduction

Simple Http

This is a bare bones http client for scala which wraps HttpURLConnection

Usage

Simple Get


  import scalaj.http.Http
  
  Http("http://foo.com/search").param("q","monkeys").asString

Simple Post

Http.post("http://foo.com/add").params("name" -> "jon", "age" -> "29").asString

OAuth Dance and Request


  import scalaj.http.{Http, Token}
  
  val consumer = Token("key", "secret")
  val token = Http("http://foursquare.com/oauth/request_token").param("oauth_callback","oob").oauth(consumer).asToken
  
  println("Go to http://foursquare.com/oauth/authorize?oauth_token=" + token.key)
  
  val verifier = Console.readLine("Enter verifier: ").trim

  val accessToken = Http("http://foursquare.com/oauth/access_token").oauth(consumer, token, verifier).asToken
  
  println(Http("http://api.foursquare.com/v1/history.json").oauth(consumer, accessToken).asString)

Parsing the response

Http("http://foo.com").{responseCode, asString, asXml, asBytes, asParams}

Installation

sbt

val scalaj_http = "org.scalaj" %% "scalaj-http" % "0.2"

maven


  <dependency>
    <groupId>org.scalaj</groupId>
    <artifactId>scalaj-http_${scala.version}</artifactId>
    <version>0.2</version>
  </dependency>  

Advanced Usage Examples

Parse the response InputStream using Lift Json


  import java.io.InputStreamReader
  import net.liftweb.json.JsonParser
  
  Http("http://foo.com"){inputStream => 
    JsonParser.parse(new InputStreamReader(is))
  }

Post raw Array[Byte] or String data and get response code

Http.postData(url, data).header("content-type", "application/json").responseCode

Post multipart/form-data

Http.multipart(url, MultiPart("photo","headshot.png", "image/png", fileBytes)).responseCode

Send https request to site with self-signed or otherwise shady certificate

Http("https://localhost/").option(HttpOptions.allowUnsafeSSL).asString

Do a HEAD request

Http(url).option(HttpOptions.method("HEAD")).asString

Custom connect and read timeouts

These are set to 30,000 and 60,000 milliseconds respectively by default

Http(url).option(HttpOptions.connTimeout(1000)).option(HttpOptions.readTimeout(5000)).asString

Other custom options

The .option() method takes a function of type HttpURLConnection => Unit so you can manipulate the connection in whatever way you want before the request executes.

TODOS

  • Retrieve triple of (statuscode, responseheaders, content)
  • Handle other http methods better (PUT,HEAD,DELETE)

scalaj-http's People

Contributors

hoffrocket avatar jonshea avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.