{"id":13682739,"url":"https://github.com/scalaj/scalaj-http","last_synced_at":"2025-04-30T09:34:04.402Z","repository":{"id":1029350,"uuid":"857520","full_name":"scalaj/scalaj-http","owner":"scalaj","description":"Simple scala wrapper for HttpURLConnection.  OAuth included.","archived":true,"fork":false,"pushed_at":"2022-04-03T21:19:24.000Z","size":645,"stargazers_count":974,"open_issues_count":28,"forks_count":120,"subscribers_count":43,"default_branch":"master","last_synced_at":"2024-08-02T13:34:27.973Z","etag":null,"topics":["http","oauth","scala"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scalaj.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-08-23T20:05:19.000Z","updated_at":"2024-07-13T16:03:43.000Z","dependencies_parsed_at":"2022-07-06T03:00:08.300Z","dependency_job_id":null,"html_url":"https://github.com/scalaj/scalaj-http","commit_stats":null,"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalaj%2Fscalaj-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalaj%2Fscalaj-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalaj%2Fscalaj-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalaj%2Fscalaj-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scalaj","download_url":"https://codeload.github.com/scalaj/scalaj-http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224206202,"owners_count":17273406,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","oauth","scala"],"created_at":"2024-08-02T13:01:52.301Z","updated_at":"2024-11-12T02:31:40.350Z","avatar_url":"https://github.com/scalaj.png","language":"Scala","funding_links":[],"categories":["Scala","HTTP","网络编程"],"sub_categories":["Spring Cloud框架"],"readme":"[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.scalaj/scalaj-http_2.12/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.scalaj/scalaj-http_2.12)\n[![Javadocs](https://javadoc.io/badge/org.scalaj/scalaj-http_2.12.svg)](https://javadoc.io/doc/org.scalaj/scalaj-http_2.12)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fscalaj%2Fscalaj-http.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fscalaj%2Fscalaj-http?ref=badge_shield)\n\n# THIS LIBRARY IS DEPRECATED\n\nAs of April 2022 there are many supported options for making HTTP calls in scala:\n\n- [sttp](https://github.com/softwaremill/sttp) or [http4s](https://github.com/http4s/http4s) might be good scala first options\n- The native [HttpClient](https://docs.oracle.com/en/java/javase/12/docs/api/java.net.http/java/net/http/HttpClient.html) library if you're using JDK 11+ and want minimal deps\n\nIf you've inherited a codebase that's using this library, I would suggest just copying the source code\n(it's only four files, and no depedencies) and make whatever fixes/changes you want.\n\nGood luck!\n\n# Simplified Http\n\nThis is a fully featured http client for Scala which wraps [java.net.HttpURLConnection](https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html)\n\n\nFeatures:\n* Zero dependencies\n* Cross compiled for Scala 2.10, 2.11, 2.12, and 2.13-M3\n* OAuth v1 request signing\n* Automatic support of gzip and deflate encodings from server\n* Easy to add querystring or form params. URL encoding is handled for you.\n* Multipart file uploads\n\nNon-Features:\n* Async execution\n  * The library is thread safe. HttpRequest and HttpResponse are immutable. So it should be easy to wrap in an execution framework of your choice.\n\nWorks in Google AppEngine and Android environments.\n\n\nBig differences:\n* Executing the request always returns a HttpResponse[T] instance that contains the response-code, headers, and body\n* Exceptions are no longer thrown for 4xx and 5xx response codes. Yay!\n* Http(url) is the starting point for every type of request (post, get, multi, etc)\n* You can easily create your own singleton instance to set your own defaults (timeouts, proxies, etc)\n* Sends \"Accept-Encoding: gzip,deflate\" request header and decompresses based on Content-Encoding (configurable)\n* Redirects are no longer followed by default. Use .option(HttpOptions.followRedirects(true)) to change.\n\n## Installation\n\n### in your build.sbt\n\n```scala\nlibraryDependencies +=  \"org.scalaj\" %% \"scalaj-http\" % \"2.4.2\"\n```\n\n### maven\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eorg.scalaj\u003c/groupId\u003e\n  \u003cartifactId\u003escalaj-http_${scala.version}\u003c/artifactId\u003e\n  \u003cversion\u003e2.4.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nIf you're including this in some other public library. Do your users a favor and change the fully qualified name\nso they don't have version conflicts if they're using a different version of this library.\nThe easiest way to do that is just to copy the source into your project :)\n\n## Usage\n\n### Simple Get\n\n```scala\nimport scalaj.http._\n\nval response: HttpResponse[String] = Http(\"http://foo.com/search\").param(\"q\",\"monkeys\").asString\nresponse.body\nresponse.code\nresponse.headers\nresponse.cookies\n```\n\n### Immutable Request\n\n```Http(url)``` is just shorthand for a ```Http.apply``` which returns an immutable instance of ```HttpRequest```.\nYou can create a ```HttpRequest``` and reuse it:\n\n```scala\nval request: HttpRequest = Http(\"http://date.jsontest.com/\")\n\nval responseOne = request.asString\nval responseTwo = request.asString\n```\n\n#### Additive Request\n\nAll the \"modification\" methods of a ```HttpRequest``` are actually returning a new instance. The param(s), option(s), header(s)\nmethods always add to their respective sets. So calling ```.headers(newHeaders)``` will return a ```HttpRequest``` instance\nthat has ```newHeaders``` appended to the previous ```req.headers```\n\n\n### Simple form encoded POST\n\n```scala\nHttp(\"http://foo.com/add\").postForm(Seq(\"name\" -\u003e \"jon\", \"age\" -\u003e \"29\")).asString\n```\n\n### OAuth v1 Dance and Request\n\n*Note: the `.oauth(...)` call must be the last method called in the request construction*\n\n```scala\nimport scalaj.http.{Http, Token}\n\nval consumer = Token(\"key\", \"secret\")\nval response = Http(\"https://api.twitter.com/oauth/request_token\").postForm(Seq(\"oauth_callback\" -\u003e \"oob\"))\n  .oauth(consumer).asToken\n\nprintln(\"Go to https://api.twitter.com/oauth/authorize?oauth_token=\" + response.body.key)\n\nval verifier = Console.readLine(\"Enter verifier: \").trim\n\nval accessToken = Http(\"https://api.twitter.com/oauth/access_token\").postForm.\n  .oauth(consumer, response.body, verifier).asToken\n\nprintln(Http(\"https://api.twitter.com/1.1/account/settings.json\").oauth(consumer, accessToken.body).asString)\n```\n\n### Parsing the response\n\n```scala\nHttp(\"http://foo.com\").{asString, asBytes, asParams}\n```\nThose methods will return an ```HttpResponse[String | Array[Byte] | Seq[(String, String)]]``` respectively\n\n## Advanced Usage Examples\n\n### Parse the response InputStream directly\n\n```scala\nval response: HttpResponse[Map[String,String]] = Http(\"http://foo.com\").execute(parser = {inputStream =\u003e\n  Json.parse[Map[String,String]](inputStream)\n})\n```\n\n### Post raw Array[Byte] or String data and get response code\n\n```scala\nHttp(url).postData(data).header(\"content-type\", \"application/json\").asString.code\n```\n\n### Post multipart/form-data\n\n```scala\nHttp(url).postMulti(MultiPart(\"photo\", \"headshot.png\", \"image/png\", fileBytes)).asString\n```\n\nYou can also stream uploads and get a callback on progress:\n\n```scala\nHttp(url).postMulti(MultiPart(\"photo\", \"headshot.png\", \"image/png\", inputStream, bytesInStream,\n  lenWritten =\u003e {\n    println(s\"Wrote $lenWritten bytes out of $bytesInStream total for headshot.png\")\n  })).asString\n```\n\n### Stream a chunked transfer response (like an event stream)\n\n```scala\nHttp(\"http://httpbin.org/stream/20\").execute(is =\u003e {\n  scala.io.Source.fromInputStream(is).getLines().foreach(println)\n})\n```\n\n_note that you may have to wrap in a while loop and set a long readTimeout to stay connected_\n\n### Send https request to site with self-signed or otherwise shady certificate\n\n```scala\nHttp(\"https://localhost/\").option(HttpOptions.allowUnsafeSSL).asString\n```\n\n### Do a HEAD request\n\n```scala\nHttp(url).method(\"HEAD\").asString\n```\n\n### Custom connect and read timeouts\n\n_These are set to 1000 and 5000 milliseconds respectively by default_\n\n```scala\nHttp(url).timeout(connTimeoutMs = 1000, readTimeoutMs = 5000).asString\n```\n\n### Get request via a proxy\n\n```scala\nval response = Http(url).proxy(proxyHost, proxyPort).asString\n```\n\n### Other custom options\n\nThe ```.option()``` method takes a function of type ```HttpURLConnection =\u003e Unit``` so\nyou can manipulate the connection in whatever way you want before the request executes.\n\n### Change the Charset\n\nBy default, the charset for all param encoding and string response parsing is UTF-8. You\ncan override with charset of your choice:\n\n```scala\nHttp(url).charset(\"ISO-8859-1\").asString\n```\n\n### Create your own HttpRequest builder\n\nYou don't have to use the default Http singleton. Create your own:\n\n```scala\nobject MyHttp extends BaseHttp (\n  proxyConfig = None,\n  options = HttpConstants.defaultOptions,\n  charset = HttpConstants.utf8,\n  sendBufferSize = 4096,\n  userAgent = \"scalaj-http/1.0\",\n  compress = true\n)\n```\n\n### Full API documentation\n\n[scaladocs here](https://javadoc.io/doc/org.scalaj/scalaj-http_2.12)\n\n## Dealing with annoying java library issues\n#### Overriding the `Access-Control, Content-Length, Content-Transfer-Encoding, Host, Keep-Alive, Origin, Trailer, Transfer-Encoding, Upgrade, Via` headers\nSome 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:\n   * Start your JVM with this command line parameter: `-Dsun.net.http.allowRestrictedHeaders=true`\n   * or, do this first thing at runtime: `System.setProperty(\"sun.net.http.allowRestrictedHeaders\", \"true\")`\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fscalaj%2Fscalaj-http.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fscalaj%2Fscalaj-http?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscalaj%2Fscalaj-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscalaj%2Fscalaj-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscalaj%2Fscalaj-http/lists"}