Dispatch 0.9的基本用法 [英] Basic usage of Dispatch 0.9

查看:209
本文介绍了Dispatch 0.9的基本用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发布到URL,设置查询参数和标题,并在正文中传递原始字符串.然后,我想执行请求并获取输出字符串,以便可以将其转换为JSON.我还希望异常处理能够响应不同类型的错误(并处理重定向)

I want to POST to a URL, setting query parameters and headers and passing a raw string in the body. I then want to do the request and get the output string, so that i can convert it to JSON. I also want exception handling to respond to different kinds of errors (and handle redirects)

但是Dispatch 0.9的文档记录很差,它用已记录的版本破坏了API,并且非常古怪,因此我无法提出一个完整的解决方案.我完全被困,因此我要求很多.

But Dispatch 0.9 is badly documented, breaks API with documented versions and is very quirky, so I cannot come up with a complete solution. I am utterly stuck, hence I ask for a lot.

这就是我能想到的,但是设置查询参数很奇怪:

This is all I can come up with, but setting the query params is weird:

val solr = host("localhost", 8983)
val req  = solr / "update" / "json" 
    setQueryParameters( Map( "commit" -> "true")) 
    setHeader( "Content-type", "application/json")
    setBody( a)

但是设置查询参数会给我带来一个棘手的错误:

But setting the query parameters gives me a tough error:

<console>:14: error: type mismatch;
 found   : scala.collection.immutable.Map[java.lang.String,java.lang.String]
 required: com.ning.http.client.FluentStringsMap
       val req  = solr / "update" / "json"  setQueryParameters( Map( "commit" -> "true"))


请帮助您完全设置请求:HTTPS,重定向,查询参数,标头和POST方法.


Please help with setting the request completely: HTTPS, redirects, query parameters, headers and the POST method.

(同时,我也希望这样)帮助执行请求以获取正文(和标头)并根据响应代码(200、301、302、400、500)分支.

Also help with (synchronously, I want it that way) executing the request to get the body (and headers) and branching depending on the response code (200, 301, 302, 400, 500).

推荐答案

距离不太远.以下应该适用于您的请求定义:

You're not too far off. The following should work for your request definition:

import dispatch._

val params = Map("commit" -> "true")
val headers = Map("Content-type" -> "application/json")

val solr = host("localhost", 8983)

val req = solr / "update" / "json" << a <<? params <:< headers

或者,较不易操作:

val req = url("http://localhost:8983/update/json").POST
  .setBody(a)
  .addQueryParameter("commit", "true")
  .addHeader("Content-type", "application/json")

如果要使用HTTPS,请在某处扔一个.secure.

Throw a .secure in there somewhere if you want to use HTTPS.

您可以像这样获得承诺(基本上代表了延迟的请求操作的结果):

You can get a promise (which essentially represents the result of a deferred request operation) like this:

val result = Http(req OK as.String).either

然后像这样使用它,例如:

And then use it like this, for example:

result() match {
  case Right(content)         => println("Content: " + content)
  case Left(StatusCode(404))  => println("Not found")
  case Left(StatusCode(code)) => println("Some other code: " + code.toString)
}

虽然我同意 0.9文档在某些地方很少,但确实提供了有关如何用promise处理异步请求操作.

While I agree that the 0.9 documentation is sparse in some places, it does provide a very useful explanation of how to handle asynchronous request operations with promises.

OP的附加功能:这一点对我来说是一个完整的答案.这样可以以一种简单的阻止方式获得响应.

OP's addition: this bit completes this answer for me. This gets the response in a simple, blocking way.

val response = Http(req)()
val body response.getResponseBody

这篇关于Dispatch 0.9的基本用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆