Questions tagged [dispatch-scala]

Dispatch is a library for asynchronous HTTP interaction. It provides a Scala vocabulary for Java’s async-http-client. The latest release version is 0.11.2.

From Dispatch Web page a brief introduction

Diving in

If you have sbt installed, Dispatch is two steps away. Open a shell and change to an empty or unimportant directory, then paste:

echo 'libraryDependencies += 
  "net.databinder.dispatch" %% "dispatch-core" % "0.11.2"' > build.sbt
sbt console

After “the internet” has downloaded, you’re good to go. Defining requests

We’ll start with a very simple request.

import dispatch., Defaults. val svc = url("http://api.hostip.info/country.php") val country = Http(svc OK as.String)

The above defines and initiates a request to the given host where 2xx responses are handled as a string. Since Dispatch is fully asynchronous, country represents a future of the string rather than the string itself. Deferring action

You can act on the response once it’s available with a for-expression.

for (c <- country)
  println(c)

This for-expression applies to any successful response that is eventually produced. If no successful response is produced, nothing is printed. This is how for-expressions work in general. Consider a more familiar example:

val opt: Option[String] = None
for (o <- opt)
  println(o)

An option may or may not contain a value, just like a future may or may not produce a successful response. But while any given option already knows what it is, a future may not. So the future behaves asynchronously in for-expressions, to avoid holding up operations subsequent that do not depend on its value. Demanding answers

As with options, you can require that a future value be available at any time:

val c = country()

But the wise use of futures defers this operation as long as is practical, or doesn’t perform it at all. To see how, keep reading.

2 questions
0
votes
1 answer

ERROR when using SPARK : java.lang.NoClassDefFoundError: dispatch/Http

When using spark-submit this error always happen: ERROR Executor: Exception in task 0.0 in stage 137.0 (TID 35) java.lang.NoClassDefFoundError: dispatch/Http$ although I have import the lib (I'm using scala) : import dispatch._, Defaults._ Part…
0
votes
1 answer

Scala Dispatch Set content-Length

I have an http post call with dispatch like this, url(API_HOST + URL).POST <:< headers <:< Map("Content-Type" -> "application/x-www-form-urlencoded") << "name=SomeName&parentId=SomeParentID" To add the Content-Lenght,…
anquegi
  • 11,125
  • 4
  • 51
  • 67