SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
806 stars 171 forks source link

NullpointerException in PutFileHandler #23

Closed shful closed 9 years ago

shful commented 9 years ago

Version: 0.10.0 This is reproduceable with three steps using httpie: 1.Create a database, 2.create a file bucket and 3.PUT binary data:

http -v -a a:a PUT 10.14.0.180:10084/testdb desc="Test Database"
http -v -a a:a PUT 10.14.0.180:10084/testdb/testbucket.files desc="My first restheart file bucket"
http -v -a a:a PUT 10.14.0.180:10084/testdb/testbucket.files/testpicture Content-Type:multipart/form-data < testpicture.jpg

Request output

PUT /testdb/testbucket.files/testpicture HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate Authorization: Basic YTph Connection: keep-alive Content-Length: 448012 Content-Type: multipart/form-data Host: 10.14.0.180:10084 User-Agent: HTTPie/0.9.2

Response shown by httpie

+-----------------------------------------+ | NOTE: binary data not shown in terminal | +-----------------------------------------+

HTTP/1.1 500 Internal Server Error Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: * Access-Control-Expose-Headers: Location Auth-Token: 2e4b48a7-62d3-4cb7-871a-87ad2177092a Auth-Token-Location: /_authtokens/a Auth-Token-Valid-Until: 2015-04-08T11:24:31.866Z Connection: keep-alive Content-Encoding: gzip Content-Length: 681 Content-Type: application/hal+json Date: Wed, 08 Apr 2015 11:09:31 GMT

{ "_embedded": { "rh:exception": [ { "_links": { "self": { "href": "#" } }, "exception": "java.lang.NullPointerException", "stack trace": [ "java.lang.NullPointerException", " at org.restheart.handlers.files.PutFileHandler.handleRequest(PutFileHandler.java:66)", " at org.restheart.handlers.RequestDispacherHandler.handleRequest(RequestDispacherHandler.java:192)", " at org.restheart.handlers.metadata.MetadataEnforcerHandler.handleRequest(MetadataEnforcerHandler.java:47)", " at org.restheart.handlers.injectors.BodyInjectorHandler.handleRequest(BodyInjectorHandler.java:104)", " at org.restheart.handlers.injectors.CollectionPropsInjectorHandler.handleRequest(CollectionPropsInjectorHandler.java:83)", " at org.restheart.handlers.injectors.DbPropsInjectorHandler.handleRequest(DbPropsInjectorHandler.java:83)", " at org.restheart.handlers.PipedWrappingHandler.handleRequest(PipedWrappingHandler.java:67)", " at org.restheart.security.handlers.SecurityHandler.handleRequest(SecurityHandler.java:60)", " at org.restheart.handlers.OptionsHandler.handleRequest(OptionsHandler.java:57)", " at org.restheart.handlers.injectors.RequestContextInjectorHandler.handleRequest(RequestContextInjectorHandler.java:239)", " at org.restheart.security.handlers.CORSHandler.handleRequest(CORSHandler.java:75)", " at org.restheart.security.handlers.AuthTokenInjecterHandler.handleRequest(AuthTokenInjecterHandler.java:72)", " at org.restheart.handlers.PipedHttpHandler.handleRequest(PipedHttpHandler.java:75)", " at io.undertow.server.handlers.PathHandler.handleRequest(PathHandler.java:56)", " at io.undertow.server.handlers.HttpContinueAcceptingHandler.handleRequest(HttpContinueAcceptingHandler.java:78)", " at org.restheart.handlers.ErrorHandler.handleRequest(ErrorHandler.java:50)", " at io.undertow.server.handlers.encoding.EncodingHandler.handleRequest(EncodingHandler.java:66)", " at org.restheart.handlers.GzipEncodingHandler.handleRequest(GzipEncodingHandler.java:65)", " at io.undertow.server.Connectors.executeRootHandler(Connectors.java:197)", " at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:764)", " at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)", " at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)", " at java.lang.Thread.run(Thread.java:745)" ] } ] }, "_links": { "self": { "href": "/testdb/testbucket.files/testpicture" } }, "http status code": 500, "http status description": "Internal Server Error", "message": "error handling the request" }

Note that I'm about to figure out how to PUT binary data into a file bucket. My approach may be wrong, but the server probably should not fail this way.

ujibang commented 9 years ago

You need to send a form encoded request.

With httpie you do that specifying the --form option. Check docs at https://github.com/jakubroztocil/httpie#forms

So you need to upload your file as follows:

http -v -a a:a --form PUT 10.14.0.180:10084/testdb/testbucket.files/testpicture file@testpicture.jpg properties='{"a":1}'

properties can be a json string with any data you want to attach to the file representation.

I checked your request with the current development release, and it does not lead to NPE anymore and correctly sends response code 406 NOT ACCEPTABLE sending back:

{
    "_embedded": {}, 
    "_links": {
        "self": {
            "href": "/api/userpics.files/testpicture"
        }
    }, 
    "http status code": 406, 
    "http status description": "Not Acceptable", 
    "message": "This request is not form encoded"
}
shful commented 9 years ago

The httpie upload with --form worked well. Thank you very much!