【问题标题】:Transforming a curl request (POST) with multipart/form-data into scala code将带有 multipart/form-data 的 curl 请求 (POST) 转换为 scala 代码
【发布时间】:2020-10-05 21:39:38
【问题描述】:

我有以下 curl 请求,我正在尝试在 scala 中实现。

curl -u "username" -X POST "https://post_url.com" -H "Content-Type: multipart/form-data" -F "xmlRequest=@/home/@file.xml;type=text/xml"

我尝试了以下方法,但收到了错误的请求。

val client = new DefaultHttpClient()
val requestEntity = MultipartEntityBuilder.create().addBinaryBody("xmlRequest",
  new File("/home/@file.xml")).build()
val post = new HttpPost("https://post_url.com")
post.addHeader(BasicScheme.authenticate(new
      UsernamePasswordCredentials(username, password), "UTF-8", false))
post.addHeader("Content-Type", "multipart/form-data")
post.setEntity(requestEntity)
val response = client.execute(post)
println(response.getStatusLine)

【问题讨论】:

    标签: scala curl post xmlhttprequest apache-httpclient-4.x


    【解决方案1】:

    我已经能够通过使用另一个 addBinaryBody 函数来解决我的问题。

      val client = new DefaultHttpClient()
      val requestEntity = MultipartEntityBuilder.create().addBinaryBody("xmlRequest",
      new File("/home/@file.xml"), ContentType.DEFAULT_BINARY, "").build()
      val post = new HttpPost("https://post_url.com")
      post.addHeader(BasicScheme.authenticate(new
      UsernamePasswordCredentials(username, password), "UTF-8", false))
      post.addHeader("Content-Type", "multipart/form-data")
      post.setEntity(requestEntity)
      val response = client.execute(post)
      println(response.getStatusLine)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2021-08-30
      相关资源
      最近更新 更多