上载带有Scala和设置标题的文件
我一直在玩一个文件上传到曲棍球应用程序很长一段时间了。我现在设法让那东西运转起来,但我撞到了下一堵墙。无法识别我设置的标题。您可以在此处找到完整的代码:但受影响的类是以下类:上载带有Scala和设置标题的文件,scala,file-upload,http-headers,hockeyapp,Scala,File Upload,Http Headers,Hockeyapp,我一直在玩一个文件上传到曲棍球应用程序很长一段时间了。我现在设法让那东西运转起来,但我撞到了下一堵墙。无法识别我设置的标题。您可以在此处找到完整的代码:但受影响的类是以下类: package de.firegate.tools import java.io.File import java.net.URI import org.apache.http.client.methods.{HttpPost, CloseableHttpResponse} import org.apache.http.
package de.firegate.tools
import java.io.File
import java.net.URI
import org.apache.http.client.methods.{HttpPost, CloseableHttpResponse}
import org.apache.http.client.protocol.HttpClientContext
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.content.{StringBody, FileBody}
import org.apache.http.entity.ContentType
import org.apache.http.util.EntityUtils
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.impl.client.HttpClients
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
object Uploader {
lazy val httpClient = {
val connManager = new PoolingHttpClientConnectionManager()
HttpClients.custom().setConnectionManager(connManager).build()
}
def run(uri: URI, file: File, properties: Map[String, String], header: Map[String, String]): Future[Try[String]] = {
import ExecutionContext.Implicits.global
Future {
Try({
// Create the entity
val reqEntity = MultipartEntityBuilder.create()
// Attach the file
reqEntity.addPart("ipa", new FileBody(file))
for ((key, value) <- properties) {
reqEntity.addPart(key, new StringBody(value, ContentType.TEXT_PLAIN))
}
// Create POST request
val httpPost = new HttpPost(uri)
httpPost.setEntity(reqEntity.build())
for ((name, value) <- properties) {
httpPost.addHeader(name, value)
}
println(httpPost.getAllHeaders.flatMap(x => x.getName + ":" + x.getValue))
// Execute the request in a new HttpContext
val ctx = HttpClientContext.create()
val response: CloseableHttpResponse = httpClient.execute(httpPost, ctx)
// Read the response
val entity = response.getEntity
val result = EntityUtils.toString(entity)
// Close the response
if (response != null) response.close()
result
})
}
}
}
上传很好,但最后曲棍球应用程序抱怨缺少凭证,尽管标题已经设置好
08:29:16.315 Upload finished {"errors":{"credentials":["no api token"]}}
08:29:16.315 Upload finished {"errors":{"credentials":["no api token"]}}