我正在使用一个用例,我需要通过Azkaban提供的AJAX APi调用Azkaban,就像
curl -k -X POST --data "session.id=9089beb2-576d-47e3-b040-86dbdc7f523e&name=aaaa&description=11" https://localhost:8443/manager?action=create
现在如何使用akka HTTP rest服务发送带有--data
参数的有效负载
即--data "session.id=9089beb2-576d-47e3-b040-86dbdc7f523e&name=aaaa&description=11"
部分
我尝试使用RequestBuilding
object AzkabanTest extends App{ implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() implicit val executor: ExecutionContext = system.dispatcher val payloadObject ="action=login&username=amdin&password=admin" val url = "https://127.0.0.1:8443" val request = Post(url, payloadObject) val result: Future[HttpResponse] = Http() .singleRequest(request) def extract(responseFuture: Future[HttpResponse]): HttpResponse = Await.result(responseFuture, 10.seconds) val res = extract(result).entity.toString println(res) } The Result should be JsonObject but { "session.id" : "ee39c901-eac9-48cd-9b3a-b25c00e8bde2", "status" : "success" }
生成空List()
错误
Exception in thread "main" javax.net.ssl.SSLHandshakeException: General SSLEngine problem
at sun.security.ssl.Handshaker.checkThrown(Handshaker.java:1431)
at sun.security.ssl.SSLEngineImpl.checkTaskThrown(SSLEngineImpl.java:535)
at sun.security.ssl.SSLEngineImpl.writeAppRecord(SSLEngineImpl.java:1214)
at sun.security.ssl.SSLEngineImpl.wrap(SSLEngineImpl.java:1186)
at javax.net.ssl.SSLEngine.wrap(SSLEngine.java:469)
at akka.stream.impl.io.TLSActor.akka$stream$impl$io$TLSActor$$doWrap(TLSActor.scala:348)
at akka.stream.impl.io.TLSActor.akka$stream$impl$io$TLSActor$$doOutbound(TLSActor.scala:317)
at akka.stream.impl.io.TLSActor$$anonfun$1.apply$mcV$sp(TLSActor.scala:228)
at akka.stream.impl.Pump$class.pump(Transfer.scala:199)
at akka.stream.impl.io.TLSActor.pump(TLSActor.scala:48)
at akka.stream.impl.BatchingInputBuffer.enqueueInputElement(ActorProcessor.scala:90)
at akka.stream.impl.BatchingInputBuffer$$anonfun$upstreamRunning$1.applyOrElse(ActorProcessor.scala:141)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
at akka.stream.impl.SubReceive.apply(Transfer.scala:16)
at akka.stream.impl.FanIn$InputBunch$$anonfun$subreceive$1.applyOrElse(FanIn.scala:234)
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
at akka.stream.impl.SubReceive.apply(Transfer.scala:16)
at akka.stream.impl.SubReceive.apply(Transfer.scala:12)
at scala.PartialFunction$class.applyOrElse(PartialFunction.scala:123)
at akka.stream.impl.SubReceive.applyOrElse(Transfer.scala:12)
at scala.PartialFunction$OrElse.applyOrElse(PartialFunction.scala:170)
at akka.actor.Actor$class.aroundReceive(Actor.scala:484)
at akka.stream.impl.io.TLSActor.aroundReceive(TLSActor.scala:48)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
at akka.actor.ActorCell.invoke(ActorCell.scala:495)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
at akka.dispatch.Mailbox.run(Mailbox.scala:224)
at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: javax.net.ssl.SSLHandshakeException: General SSLEngine problem
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1728)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:304)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker$1.run(Handshaker.java:919)
at sun.security.ssl.Handshaker$1.run(Handshaker.java:916)
at java.security.AccessController.doPrivileged(Native Method)
at sun.security.ssl.Handshaker$DelegatedTask.run(Handshaker.java:1369)
at akka.stream.impl.io.TLSActor.runDelegatedTasks(TLSActor.scala:402)
at akka.stream.impl.io.TLSActor.akka$stream$impl$io$TLSActor$$doUnwrap(TLSActor.scala:371)
at akka.stream.impl.io.TLSActor.akka$stream$impl$io$TLSActor$$doInbound(TLSActor.scala:290)
at akka.stream.impl.io.TLSActor$$anonfun$1.apply$mcV$sp(TLSActor.scala:225)
... 24 more
Caused by: java.security.cert.CertificateException: No subject alternative names present
at sun.security.util.HostnameChecker.matchIP(HostnameChecker.java:144)
at sun.security.util.HostnameChecker.match(HostnameChecker.java:93)
at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:455)
at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:436)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:252)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:136)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1496)
... 34 more
答案 0 :(得分:0)
如果您打算仅发送单个请求,则应使用Request-Level Client-Side API Post
请求:
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding._
val request = Post(url, payloadObject)
val result: Future[YourTargetResult] = Http()
.singleRequest(request)
.flatMap(handleResponse)
def handleResponse(response: HttpResponse): YourTargetResult = ...
payloadObject
应根据所选格式进行序列化。在您的特定情况下,您可能希望将其设置为具有所需有效负载的字符串。在需要XML / JSON的更复杂的情况下,您需要将编组器提供为所需的格式(即Spray JSON)。
UPD :根据curl man page,为了构建类似于curl对多个--data
参数的有效负载,只需将值连接到{{1 }}:
-d, - data
如果在同一命令行中多次使用这些选项中的任何一个,则指定的数据将与分离& -symbol合并在一起。因此,使用'-d name = daniel -d skill = lousy'会生成一个看起来像'name = daniel& skill = lousy'的帖子块。