使用 HTTPie 或 Curl 通过 JHipster REST 服务加载文件
Load a file via JHipster REST service using HTTPie or Curl
我制作了一个 JHipster (v5.3.1) 应用程序,它有一个实体 Attachment
,其中包含一个名为 fileAttachment
的 Blob
字段。使用生成的 Angular 6 Web 前端,我能够将文件加载到应用程序。我想使用命令行(HTTPie
或 curl
)做同样的事情。
附件的jdl定义是:
/**
* An entity to store attachments
*/
entity Attachment {
/** The name of the attachment */
fileName String required,
/** the attachment */
attachedFile Blob required
}
JHipster 生成器构造了一个 Attachment.java
和 AttachmentService.java
以及一个我根本没有修改过的 AttachmentResource.java
。
使用 Web 前端,加载文件时会记录以下语句:
2018-09-18 09:18:11.501 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Enter: com.kaleido.kapture.web.rest.AttachmentResource.createAttachment() with argument[s] = [Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}]
2018-09-18 09:18:11.502 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.web.rest.AttachmentResource : REST request to save Attachment : Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
2018-09-18 09:18:11.504 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Enter: com.kaleido.kapture.service.AttachmentService.save() with argument[s] = [Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}]
2018-09-18 09:18:11.518 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.service.AttachmentService : Request to save Attachment : Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
Hibernate: call next value for hibernate_sequence
2018-09-18 09:18:11.562 DEBUG 14151 --- [ XNIO-2 task-7] o.j.core.metamodel.type.TypeFactory : javersType of class com.kaleido.kapture.domain.Attachment inferred as EntityType
2018-09-18 09:18:11.564 DEBUG 14151 --- [ XNIO-2 task-7] o.j.core.metamodel.type.TypeFactory : javersType of class java.lang.Long inferred as ValueType, it's used as id-property type
2018-09-18 09:18:11.597 DEBUG 14151 --- [ XNIO-2 task-7] o.j.core.metamodel.type.TypeFactory : javersType of class [B spawned as ArrayType from prototype ArrayType{baseType:'class [Ljava.lang.Object;'}
2018-09-18 09:18:11.599 DEBUG 14151 --- [ XNIO-2 task-7] o.javers.core.graph.ObjectGraphBuilder : live graph assembled, object nodes: 1, entities: 1, valueObjects: 0
2018-09-18 09:18:11.695 INFO 14151 --- [ XNIO-2 task-7] org.javers.core.Javers : Commit(id:1.0, snapshots:1, author:admin, changes - NewObject:1), done in 144 millis (diff:71, persist:73)
2018-09-18 09:18:12.096 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Exit: com.kaleido.kapture.service.AttachmentService.save() with result = Attachment{id=1001, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
Hibernate: insert into attachment (attached_file, attached_file_content_type, file_name, id) values (?, ?, ?, ?)
2018-09-18 09:18:12.104 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Exit: com.kaleido.kapture.web.rest.AttachmentResource.createAttachment() with result = <201 Created,Attachment{id=1001, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'},{Location=[/api/attachments/1001], X-kaptureApp-alert=[A new attachment is created with identifier 1001], X-kaptureApp-params=[1001]}>
使用 HTTPie 我能够进行身份验证并获得 JWT 令牌
http POST :8080/api/authenticate password=**** username=admin
但是,如何POST
文件并不明显:
http -v --form POST :8080/api/attachments/ \
'Authorization:Bearer ***.***.***' \
fileName=test.txt \
attachedFile@test.txt \
attachedFileContentType=text/plain
结果:
POST /api/attachments/ HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Bearer ****.****.****
Connection: keep-alive
Content-Length: 404
Content-Type: multipart/form-data;
boundary=087aa239fc6f4eb197fa883424e2bdc3
Host: localhost:8080
User-Agent: HTTPie/0.9.9
--087aa239fc6f4eb197fa883424e2bdc3
Content-Disposition: form-data; name="fileName"
test.txt
--087aa239fc6f4eb197fa883424e2bdc3
Content-Disposition: form-data; name="attachedFileContentType"
text/plain
--087aa239fc6f4eb197fa883424e2bdc3
Content-Disposition: form-data; name="attachedFile";
filename="test.txt"
Content-Type: text/plain
test test test
--087aa239fc6f4eb197fa883424e2bdc3--
HTTP/1.1 415 Unsupported Media Type
Accept: application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/x-jackson-smile, application/*+xml, multipart/form-data, application/json, application/cbor, application/*+json, */*
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/problem+json
Date: Tue, 18 Sep 2018 14:27:08 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
{
"detail": "Content type 'multipart/form-data;boundary=087aa239fc6f4eb197fa883424e2bdc3;charset=UTF-8' not supported",
"message": "error.http.415",
"path": "/api/attachments/",
"status": 415,
"title": "Unsupported Media Type",
"type": "https://www.jhipster.tech/problem/problem-with-message"
}
我应该如何构造POST
?
更新
当我在 Chrome 开发人员视图中查看发生的情况时,我看到请求负载是
{"fileName":"test.txt","attachedFileContentType":"text/plain","attachedFile":"dGVzdCB0ZXN0IHRlc3QK"}
不清楚 attachedFile
值的来源?
WebUI生成的请求头是:
POST /api/attachments HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 100
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://localhost:8080
X-XSRF-TOKEN: *****************
Authorization: Bearer ****.****.****.****
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36
Content-Type: application/json
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie:
access_token=****.****.****; session_token=****; XSRF-TOKEN=*****; Idea-1c443776=2050c190-4f64-4a09-a50c-a0ef97b6a9da; io=p4GYHllrvXuakjfbAAAA
解决方案是对文件进行 Base64 编码,并提供该字符串作为 attachedFile
的值(感谢 Jon Ruddell 的提示)。
以下调用将创建一个记录,其中包含一个名为 test.txt
的附件,其内容采用 base64 编码“test test test
”,内容类型为 text/plain
。
http POST :8080/api/attachments/ \
'Authorization:Bearer ****.****.****' \
fileName=test.txt \
attachedFile=dGVzdCB0ZXN0IHRlc3QK \
attachedFileContentType=text/plain
对于任何真实文件,在命令行中包含文件的 Base64 编码是不现实的。因此你
Base64 应该将内容编码到文件中,然后使用 http
引用该文件,从而使其成为 JSON 的一部分
body.
编码一个文件(schema.png
)并将编码写入另一个文件(schema.png.base64
):
openssl base64 -in schema.png > schema.png.base64
POST
名称为 schema2.png
且媒体类型为 image/png
的文件
http POST :8080/api/attachments/ \
'Authorization:Bearer ****.****.***' \
fileName=schema2.png \
attachedFile=@schema.png.base64 \
attachedFileContentType=image/png
我制作了一个 JHipster (v5.3.1) 应用程序,它有一个实体 Attachment
,其中包含一个名为 fileAttachment
的 Blob
字段。使用生成的 Angular 6 Web 前端,我能够将文件加载到应用程序。我想使用命令行(HTTPie
或 curl
)做同样的事情。
附件的jdl定义是:
/**
* An entity to store attachments
*/
entity Attachment {
/** The name of the attachment */
fileName String required,
/** the attachment */
attachedFile Blob required
}
JHipster 生成器构造了一个 Attachment.java
和 AttachmentService.java
以及一个我根本没有修改过的 AttachmentResource.java
。
使用 Web 前端,加载文件时会记录以下语句:
2018-09-18 09:18:11.501 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Enter: com.kaleido.kapture.web.rest.AttachmentResource.createAttachment() with argument[s] = [Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}]
2018-09-18 09:18:11.502 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.web.rest.AttachmentResource : REST request to save Attachment : Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
2018-09-18 09:18:11.504 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Enter: com.kaleido.kapture.service.AttachmentService.save() with argument[s] = [Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}]
2018-09-18 09:18:11.518 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.service.AttachmentService : Request to save Attachment : Attachment{id=null, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
Hibernate: call next value for hibernate_sequence
2018-09-18 09:18:11.562 DEBUG 14151 --- [ XNIO-2 task-7] o.j.core.metamodel.type.TypeFactory : javersType of class com.kaleido.kapture.domain.Attachment inferred as EntityType
2018-09-18 09:18:11.564 DEBUG 14151 --- [ XNIO-2 task-7] o.j.core.metamodel.type.TypeFactory : javersType of class java.lang.Long inferred as ValueType, it's used as id-property type
2018-09-18 09:18:11.597 DEBUG 14151 --- [ XNIO-2 task-7] o.j.core.metamodel.type.TypeFactory : javersType of class [B spawned as ArrayType from prototype ArrayType{baseType:'class [Ljava.lang.Object;'}
2018-09-18 09:18:11.599 DEBUG 14151 --- [ XNIO-2 task-7] o.javers.core.graph.ObjectGraphBuilder : live graph assembled, object nodes: 1, entities: 1, valueObjects: 0
2018-09-18 09:18:11.695 INFO 14151 --- [ XNIO-2 task-7] org.javers.core.Javers : Commit(id:1.0, snapshots:1, author:admin, changes - NewObject:1), done in 144 millis (diff:71, persist:73)
2018-09-18 09:18:12.096 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Exit: com.kaleido.kapture.service.AttachmentService.save() with result = Attachment{id=1001, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
Hibernate: insert into attachment (attached_file, attached_file_content_type, file_name, id) values (?, ?, ?, ?)
2018-09-18 09:18:12.104 DEBUG 14151 --- [ XNIO-2 task-7] c.k.kapture.aop.logging.LoggingAspect : Exit: com.kaleido.kapture.web.rest.AttachmentResource.createAttachment() with result = <201 Created,Attachment{id=1001, fileName='test', attachedFile='[B@396897d6', attachedFileContentType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'},{Location=[/api/attachments/1001], X-kaptureApp-alert=[A new attachment is created with identifier 1001], X-kaptureApp-params=[1001]}>
使用 HTTPie 我能够进行身份验证并获得 JWT 令牌
http POST :8080/api/authenticate password=**** username=admin
但是,如何POST
文件并不明显:
http -v --form POST :8080/api/attachments/ \
'Authorization:Bearer ***.***.***' \
fileName=test.txt \
attachedFile@test.txt \
attachedFileContentType=text/plain
结果:
POST /api/attachments/ HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Bearer ****.****.****
Connection: keep-alive
Content-Length: 404
Content-Type: multipart/form-data;
boundary=087aa239fc6f4eb197fa883424e2bdc3
Host: localhost:8080
User-Agent: HTTPie/0.9.9
--087aa239fc6f4eb197fa883424e2bdc3
Content-Disposition: form-data; name="fileName"
test.txt
--087aa239fc6f4eb197fa883424e2bdc3
Content-Disposition: form-data; name="attachedFileContentType"
text/plain
--087aa239fc6f4eb197fa883424e2bdc3
Content-Disposition: form-data; name="attachedFile";
filename="test.txt"
Content-Type: text/plain
test test test
--087aa239fc6f4eb197fa883424e2bdc3--
HTTP/1.1 415 Unsupported Media Type
Accept: application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/x-jackson-smile, application/*+xml, multipart/form-data, application/json, application/cbor, application/*+json, */*
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/problem+json
Date: Tue, 18 Sep 2018 14:27:08 GMT
Expires: 0
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
{
"detail": "Content type 'multipart/form-data;boundary=087aa239fc6f4eb197fa883424e2bdc3;charset=UTF-8' not supported",
"message": "error.http.415",
"path": "/api/attachments/",
"status": 415,
"title": "Unsupported Media Type",
"type": "https://www.jhipster.tech/problem/problem-with-message"
}
我应该如何构造POST
?
更新
当我在 Chrome 开发人员视图中查看发生的情况时,我看到请求负载是
{"fileName":"test.txt","attachedFileContentType":"text/plain","attachedFile":"dGVzdCB0ZXN0IHRlc3QK"}
不清楚 attachedFile
值的来源?
WebUI生成的请求头是:
POST /api/attachments HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 100
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://localhost:8080
X-XSRF-TOKEN: *****************
Authorization: Bearer ****.****.****.****
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36
Content-Type: application/json
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie:
access_token=****.****.****; session_token=****; XSRF-TOKEN=*****; Idea-1c443776=2050c190-4f64-4a09-a50c-a0ef97b6a9da; io=p4GYHllrvXuakjfbAAAA
解决方案是对文件进行 Base64 编码,并提供该字符串作为 attachedFile
的值(感谢 Jon Ruddell 的提示)。
以下调用将创建一个记录,其中包含一个名为 test.txt
的附件,其内容采用 base64 编码“test test test
”,内容类型为 text/plain
。
http POST :8080/api/attachments/ \
'Authorization:Bearer ****.****.****' \
fileName=test.txt \
attachedFile=dGVzdCB0ZXN0IHRlc3QK \
attachedFileContentType=text/plain
对于任何真实文件,在命令行中包含文件的 Base64 编码是不现实的。因此你
Base64 应该将内容编码到文件中,然后使用 http
引用该文件,从而使其成为 JSON 的一部分
body.
编码一个文件(schema.png
)并将编码写入另一个文件(schema.png.base64
):
openssl base64 -in schema.png > schema.png.base64
POST
名称为 schema2.png
且媒体类型为 image/png
http POST :8080/api/attachments/ \
'Authorization:Bearer ****.****.***' \
fileName=schema2.png \
attachedFile=@schema.png.base64 \
attachedFileContentType=image/png