- Notifications
You must be signed in to change notification settings - Fork119
Simple scala wrapper for HttpURLConnection. OAuth included.
License
scalaj/scalaj-http
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
As of April 2022 there are many supported options for making HTTP calls in scala:
- sttp orhttp4s might be good scala first options
- The nativeHttpClient library if you're using JDK 11+ and want minimal deps
If you've inherited a codebase that's using this library, I would suggest just copying the source code(it's only four files, and no depedencies) and make whatever fixes/changes you want.
Good luck!
This is a fully featured http client for Scala which wrapsjava.net.HttpURLConnection
Features:
- Zero dependencies
- Cross compiled for Scala 2.10, 2.11, 2.12, and 2.13-M3
- OAuth v1 request signing
- Automatic support of gzip and deflate encodings from server
- Easy to add querystring or form params. URL encoding is handled for you.
- Multipart file uploads
Non-Features:
- Async execution
- The library is thread safe. HttpRequest and HttpResponse are immutable. So it should be easy to wrap in an execution framework of your choice.
Works in Google AppEngine and Android environments.
Big differences:
- Executing the request always returns a HttpResponse[T] instance that contains the response-code, headers, and body
- Exceptions are no longer thrown for 4xx and 5xx response codes. Yay!
- Http(url) is the starting point for every type of request (post, get, multi, etc)
- You can easily create your own singleton instance to set your own defaults (timeouts, proxies, etc)
- Sends "Accept-Encoding: gzip,deflate" request header and decompresses based on Content-Encoding (configurable)
- Redirects are no longer followed by default. Use .option(HttpOptions.followRedirects(true)) to change.
libraryDependencies+="org.scalaj"%%"scalaj-http"%"2.4.2"
<dependency> <groupId>org.scalaj</groupId> <artifactId>scalaj-http_${scala.version}</artifactId> <version>2.4.2</version></dependency>
If you're including this in some other public library. Do your users a favor and change the fully qualified nameso they don't have version conflicts if they're using a different version of this library.The easiest way to do that is just to copy the source into your project :)
importscalaj.http._valresponse:HttpResponse[String]=Http("http://foo.com/search").param("q","monkeys").asStringresponse.bodyresponse.coderesponse.headersresponse.cookies
Http(url)
is just shorthand for aHttp.apply
which returns an immutable instance ofHttpRequest
.You can create aHttpRequest
and reuse it:
valrequest:HttpRequest=Http("http://date.jsontest.com/")valresponseOne= request.asStringvalresponseTwo= request.asString
All the "modification" methods of aHttpRequest
are actually returning a new instance. The param(s), option(s), header(s)methods always add to their respective sets. So calling.headers(newHeaders)
will return aHttpRequest
instancethat hasnewHeaders
appended to the previousreq.headers
Http("http://foo.com/add").postForm(Seq("name"->"jon","age"->"29")).asString
Note: the.oauth(...)
call must be the last method called in the request construction
importscalaj.http.{Http,Token}valconsumer=Token("key","secret")valresponse=Http("https://api.twitter.com/oauth/request_token").postForm(Seq("oauth_callback"->"oob")) .oauth(consumer).asTokenprintln("Go to https://api.twitter.com/oauth/authorize?oauth_token="+ response.body.key)valverifier=Console.readLine("Enter verifier:").trimvalaccessToken=Http("https://api.twitter.com/oauth/access_token").postForm. .oauth(consumer, response.body, verifier).asTokenprintln(Http("https://api.twitter.com/1.1/account/settings.json").oauth(consumer, accessToken.body).asString)
Http("http://foo.com").{asString, asBytes, asParams}
Those methods will return anHttpResponse[String | Array[Byte] | Seq[(String, String)]]
respectively
valresponse:HttpResponse[Map[String,String]]=Http("http://foo.com").execute(parser= {inputStream=>Json.parse[Map[String,String]](inputStream)})
Http(url).postData(data).header("content-type","application/json").asString.code
Http(url).postMulti(MultiPart("photo","headshot.png","image/png", fileBytes)).asString
You can also stream uploads and get a callback on progress:
Http(url).postMulti(MultiPart("photo","headshot.png","image/png", inputStream, bytesInStream, lenWritten=> { println(s"Wrote$lenWritten bytes out of$bytesInStream total for headshot.png") })).asString
Http("http://httpbin.org/stream/20").execute(is=> { scala.io.Source.fromInputStream(is).getLines().foreach(println)})
note that you may have to wrap in a while loop and set a long readTimeout to stay connected
Http("https://localhost/").option(HttpOptions.allowUnsafeSSL).asString
Http(url).method("HEAD").asString
These are set to 1000 and 5000 milliseconds respectively by default
Http(url).timeout(connTimeoutMs=1000, readTimeoutMs=5000).asString
valresponse=Http(url).proxy(proxyHost, proxyPort).asString
The.option()
method takes a function of typeHttpURLConnection => Unit
soyou can manipulate the connection in whatever way you want before the request executes.
By default, the charset for all param encoding and string response parsing is UTF-8. Youcan override with charset of your choice:
Http(url).charset("ISO-8859-1").asString
You don't have to use the default Http singleton. Create your own:
objectMyHttpextendsBaseHttp ( proxyConfig=None, options=HttpConstants.defaultOptions, charset=HttpConstants.utf8, sendBufferSize=4096, userAgent="scalaj-http/1.0", compress=true)
Overriding theAccess-Control, Content-Length, Content-Transfer-Encoding, Host, Keep-Alive, Origin, Trailer, Transfer-Encoding, Upgrade, Via
headers
Some of the headers are locked by the java library for "security" reasons and the behavior is that the library will just silently fail to set them. You can workaround by doing one of the following:
- Start your JVM with this command line parameter:
-Dsun.net.http.allowRestrictedHeaders=true
- or, do this first thing at runtime:
System.setProperty("sun.net.http.allowRestrictedHeaders", "true")
About
Simple scala wrapper for HttpURLConnection. OAuth included.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.