![]() Recipe 15.1, “ Creating a JSON String from a Scala Object” and Recipe 15.In the last lines of the code, the POST request is sent using the client.execute call, the response is received, and the response headers are printed (though this isn’t necessary). The Scala HTTP client you always wanted synchronous client Scala versions: 3.x 2.13 2.12 2.11 Scala.js versions: 1.x Scala Native versions: 0. In this example, once you’ve constructed a JSON string from a Scala object, the Apache HttpClient NameValuePair, BasicNameValuePair, and UrlEncodedFormEntity classes are used to set an Entity on an HttpPost object. For more complex cases, you’ll probably want to use the Lift-JSON library, as discussed in the first several recipes of this chapter. The Gson library is used to construct a JSON string in this code because this is a simple example. Nevertheless, none of them come close to the ease and weightlessness of using Kenneth Reitz's Requests library: too many implicits, operators, builders, monads, and other things. Akka-http, Play-WS, STTP, HTTP4S, Scalaj-HTTP, RosHTTP, Dispatch. Solution Create a JSON string using your favorite JSON library, and then send the data to the POST URL using the Apache HttpClient library. There is a whole zoo of HTTP clients in the Scala ecosystem. ![]() tEntity(new UrlEncodedFormEntity(nameValuePairs)) When writing Scala code, you want to send JSON data (or other data) to a POST URL, either from a standalone client, or when using a framework that doesn’t provide this type of service. NameValuePairs.add(new BasicNameValuePair("JSON", spockAsJson)) This is a fully featured http client for Scala which wraps 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. Val post = new HttpPost(" val nameValuePairs = new ArrayList() Val spockAsJson = new Gson().toJson(spock) Val spock = new Person("Leonard", "Nimoy", 82) In practice, the only thing I really had to look at was the RequestBuilder - everything else falling nicely into my understanding of HTTP. Import .entity.UrlEncodedFormEntityĬase class Person(firstName: String, lastName: String, age: Int) Dispatch 0.9.1 is a Scala wrapper around Ning's Async Http Client to fully understand what going on requires introducing one's self to that library. In the following example, the Gson library is used to construct a JSON string, which is then sent to a server using the methods of the HttpClient library: ![]() SolutionĬreate a JSON string using your favorite JSON library, and then send the data to the POST URL using the Apache HttpClient library. When writing Scala code, you want to send JSON data (or other data) to a POST URL, either from a standalone client, or when using a framework that doesn’t provide this type of service. Note: Some components such as HTTP/2 might have more requirements. Vert.x SQL Client - Testing with PostgreSQL, MySQL, MariaDB, SQL Server, etc. This is a short recipe, Recipe 15.11, “How to send JSON POST data to a REST URL in Scala.” Problem Netty is an NIO client server framework which enables quick and easy development of. Skyscanner - Integration testing against HTTP service mocks and various. This is an excerpt from the Scala Cookbook (partially modified for the internet). show more info on classes/objects in repl.Twitter is a fairly isolated corner of the eco-system, their libraries are brilliant but finagle-http will use Twitter Futures alongside a set of completely different concurrency primitives and so on.īest advice I can give you is if you use Akka already in your project, just go for akka-http-core, if you use play go for play-ws and so on. Truthfully all of these dependencies are pretty huge and they may use very different underlying async HTTP libraries in Java to implement on top of, which may well lead to you bringing in huge dependencies for very little benefit. This is a fully featured http client for Scala which wraps 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. In practice I would very strongly recommend that you look at what you already have in the classpath. Reboot: Dispatch reboot is a rewrite of the Dispatch library for HTTP interaction in Scala, using async-http-client as its underlying transport. Most of Finagle’s code is protocol agnostic, simplifying the implementation of new protocols. Theres a number of great Scala HTTP libraries. Finagle implements uniform client and server APIs for several protocols, and is designed for high performance and concurrency. Meetup organised by London Scala Users Group, 12th September 2017, London. ![]() The following libraries still pre-bundle a client:įinagle-http: Finagle is an extensible RPC system for the JVM, used to construct high-concurrency servers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |