【问题标题】:How to upload a file as a binary in request body of REST API using restassured Java?如何使用放心的 Java 在 REST API 的请求正文中将文件作为二进制文件上传?
【发布时间】:2021-10-15 17:09:39
【问题描述】:
curl command for my request looks like below:
curl --location --request PUT '<endpoint URI>' \
--header 'Content-Type: application/octet-stream' \
--data-binary '@/<file location>'
如何在 Java 中使用 reassured 将文件作为二进制文件传递?
【问题讨论】:
标签:
java
api
rest
binary
rest-assured
【解决方案1】:
像往常一样由我自己发现;)
高级 Java 代码:
given()
.relaxedHTTPSValidation()
.header("Content-Disposition: attachment; filename", file)
.body(file)
输出:
Request method: <HTTP METHOD>
Request URI: <endpoint URI>
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Content-Disposition: attachment; filename="<absolute filepath>"
Accept=*/*
Content-Type=text/plain; charset=ISO-8859-1
Cookies: <none>
Multiparts: <none>
Body:
<relative filePath>
net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated reportQuery
INFO: No BaseStepListener, PUT not registered.
Date: Thu, 12 Aug 2021 14:48:26 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Cache-Control: no-store
Transfer-Encoding: chunked
Location:
Content-Security-Policy: frame-ancestors 'none'
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
Response: <RESPONSE>