【问题标题】:How to send FormData In Http Post using Spray如何使用 Spray 在 Http Post 中发送 FormData
【发布时间】:2015-10-21 12:15:28
【问题描述】:

我希望我的 Spray Client 使用此内容类型发送帖子请求

Content-Type: application/x-www-form-urlencoded 

我相信我需要为此使用对象 FormData:

var fD = new FormData(Seq("UserID:" -> "123", "PWD" -> "123" , "Brand" -> "123"))

但我愿意接受其他解决方案。

编辑:

我试过这样发送:

implicit val system = ActorSystem("Client")
  var fD =  FormData(Map("UserID" -> "123", "PWD" -> "123" , "Brand" -> "123"))
  import system.dispatcher // execution context for futures below
  val log = Logging(system, getClass)
  log.info("Sending test Msg")
  val pipeline = sendReceive ~> unmarshal[FormData]
  var startTimestamp = System.currentTimeMillis()
  val responseFuture = pipeline {
    Post(url, fD)
  }
  responseFuture.onComplete(x=> println(s"Request completed in ${System.currentTimeMillis() - startTimestamp} millis.\n" +
    s"Recived :"+x.get)

  )

我得到了这个错误:

spray.httpx.PipelineException: UnsupportedContentType(Expected 'application/x-www-form-urlencoded')

我做错了什么?感谢帮助。

【问题讨论】:

    标签: scala http spray form-data


    【解决方案1】:

    您基本上已经回答了自己的问题——您会想要坚持使用 FormData。不过有几件小事:

    1. 由于您使用的是 FormData 案例类,因此您可以删除 “新”。
    2. FormData 的伴随对象将允许您传入 一个 Map 而不是一个 Seq 元组。

    【讨论】:

    • 我现在编辑并添加了我尝试过的代码并且我得到了 UnsupportedCONntentType Exeption 。也许你知道该怎么做?
    • 由于unmarshal[FormData],可能会引发该异常。 unmarshaller 期望响应将包含 application/x-www-form-urlencoded 的 Content-Type 标头,而实际上并没有。试试改成unmarshal[HttpResponse]
    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 2021-08-14
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多