// authenticate
def authSite = new HTTPBuilder( 'https://*********.objectstore.eu/' )
authSite.auth.basic '******', '******'
def filestore = new File("C:/*****")
filestore.createNewFile()
FileUtils.writeStringToFile(filestore, "datawhathwat")
//save object
authSite.request(PUT) { req ->
uri.path = '/images/********.txt'
headers.'Content-Type' = 'image/jpg'
body: "filestore"
}
authSite.shutdown()
使用此代码时, header 通过,创建了一个文件,但该文件是空的。
这是 API 文档的链接:http://developer.openstack.org/api-ref-objectstorage-v1.html#createOrReplaceObject
最佳答案
在请求正文中,您传递的是一个字符串,而不是您尝试上传的文件的内容。以这种方式将文件内容获取到正文中:
authSite.request(PUT) { req ->
...
body: filestore.bytes
}
关于swift - HTTP 请求 PUT 文件(图像)到 Swift RESTful API (Grails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31986262/