mwpastore / sinja

RESTful, {json:api}-compliant web services in Sinatra
http://sinja-rb.org
MIT License
87 stars 8 forks source link

All creates yield `Malformed {json:api} request payload` #12

Closed jgnagy closed 7 years ago

jgnagy commented 7 years ago

The issue seems to be that all my attempts to create new resources via the JSON API using sinja seem to not work. It is entirely possible that I'm doing something wrong, but I can't find it looking through both the README and demo app.

I created a repo with the most essential components of my app captured: https://github.com/jgnagy/test-api

Notice the /keys endpoint supports both show and create. The show works fine, per this curl command:

curl -i  -H 'Accept: application/vnd.api+json' http://localhost:9292/keys/1

Which yields something like:

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
Last-Modified: Mon, 13 Feb 2017 21:04:00 GMT
Content-Length: 260
Server: WEBrick/1.3.1 (Ruby/2.3.1/2016-04-26)
Date: Mon, 13 Feb 2017 21:04:00 GMT
Connection: Keep-Alive

{
  "data": {
    "type": "keys",
    "id": "1",
    "attributes": {
      "name": "A Key",
      "created-at": "2017-02-13 13:04:00 -0800"
    },
    "links": {
      "self": "/keys/1"
    }
  },
  "jsonapi": {
    "version": "1.0"
  },
  "included": [

  ]
}

However, attempting to create a new resource via this curl command:

curl -i -H 'Accept: application/vnd.api+json' -H 'Content-Type: application/vnd.api+json' --data '{"data":{"type":"keys", "attributes":{"name":"foo"}}}' http://localhost:9292/keys

Returns the following:

HTTP/1.1 400 Bad Request
Content-Type: application/vnd.api+json
Content-Length: 199
Server: WEBrick/1.3.1 (Ruby/2.3.1/2016-04-26)
Date: Mon, 13 Feb 2017 21:04:02 GMT
Connection: Keep-Alive

{
  "errors": [
    {
      "id": "0d741b83-7a52-499a-8365-c278cd161e3a",
      "title": "Bad Request Error",
      "detail": "Malformed {json:api} request payload",
      "status": "400"
    }
  ]
}

I'm uncertain why this isn't working, and I've tried making additional Sinatra apps just passing "normal" JSON around and that works fine. When I involve sinja, it seems unhappy and I can't see why.

I'm using ruby 2.3.1 (about to upgrade ruby and see if that helps) via RVM on macOS.

I appreciate the help!

jgnagy commented 7 years ago

Same behavior with 2.3.3. Will try 2.4.0 shortly.

jgnagy commented 7 years ago

Same thing with 2.4.0.

mwpastore commented 7 years ago

Hi @jgnagy, thank you very much for the issue report. It looks like the Rack::Lint::InputWrapper request object doesn't respond to #size, so Sinja sees an empty and therefore invalid payload. Let me take a look at Sinja's content? helper to see if there's a better way to handle these types of inputs.

jgnagy commented 7 years ago

Ah, I see. Your demo app doesn't use rackup, instead running app.rb directly. My actual app uses several modular Sintra applications and uses rack to route the requests to them, so there is our difference.

Thank you for responding so quickly. Let me know if there is anything I can do to help, and I look forward to the fix so I can keep moving on my application.

Thanks!

mwpastore commented 7 years ago

I think that will do the trick. Add Sinja to your Gemfile (instead of gemspec) and point it at this repo. I'll release a new version of the gem that includes this fix a little later today or tomorrow. Thanks again for the report, and please feel free to reopen this issue if it's still not fixed (or open a new issue if you find anything else wrong).

jgnagy commented 7 years ago

I hate to be a pain, but I'm still getting the same results. I updated that test repo with the change (pointing directly to your master branch in the Gemfile) and I still get the send end result. Should I open a new issue?

mwpastore commented 7 years ago

You aren't being a pain at all. I missed something. I figured out a way to test it, now, so hopefully this issue won't reappear in the future. 😄 Thank you for your patience; would you please try again?

jgnagy commented 7 years ago

Looks like that fixed it in my test version (code pushed to that repo). I'll head back to my actual app and see if I have success there as well. Thanks again for the quick turnaround!

mwpastore commented 7 years ago

You're quite welcome. Sinja 1.2.4 is now available with the fix (and a number of other fixes and improvements). Thanks again for the report!