将带有 multipart/form-data 的 curl 请求 (POST) 转换为 Scala 代码

Transforming a curl request (POST) with multipart/form-data into scala code

我试图在 Scala 中实现以下 curl 请求。

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)

我已经能够通过使用另一个 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)