将curl请求(POST)转换为Scala代码

时间:2020-06-16 09:56:31

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

我有以下要在Scala中实现的curl请求。

curl -u“用户名” -X POST“ https://post_url.com” -H“内容类型: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)

1 个答案:

答案 0 :(得分:0)

通过使用另一个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)
相关问题