swift - HTTP 请求 PUT 文件(图像)到 Swift RESTful API (Grails)

标签 swift api rest grails httprequest

// 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/

相关文章:

php - Symfony 安全组件 - 无法在 token 负载中找到 key \"username\"

java - 使用带有 Spring Boot 的 Spock 测试框架来测试我的 REST Controller

iOS swift : Send log information to iOS app from iOS framework

ios - IMGLYMainEditorViewController 完成 block 问题

javascript - 如何创建从 xpages 到 yandex 翻译 api 的 json 请求

java - 状态“:400 com. fasterxml.jackson.databind.exc.MismatchedInputException : Cannot deserialize instance of java. lang.Boolean

rest - 以 REST 方式更新整个资源集合

ios - Swift-在UITableView中添加单元格

ios - Swift NSNotificationCenter 在其他类中添加观察者功能

javascript - 回调 hell 、地理位置 - JavaScript