-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Parse JSON array data in body at top level #1730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It's an interesting problem. Without naming the params we would be getting a body, but no automatic parsing. That answers your immediate question, you can require 'spec_helper'
describe Grape::Endpoint do
subject { Class.new(Grape::API) }
def app
subject
end
before do
subject.format :json
subject.post do
JSON.parse(request.body.first)
end
end
let(:data) { ::Grape::Json.dump([{ foo: 'bar' }, { foo: 'baz' }]) }
it 'responds' do
post '/', data, 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq 201
expect(last_response.body).to eq(data)
end
end But we want to be able to declare this. With named require 'spec_helper'
describe Grape::Endpoint do
subject { Class.new(Grape::API) }
def app
subject
end
before do
subject.format :json
subject.params do
requires :foos, type: Array
end
subject.post do
params
end
end
let(:data) { ::Grape::Json.dump(foos: [{ foo: 'bar' }, { foo: 'baz' }]) }
it 'responds' do
post '/', data, 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq 201
expect(last_response.body).to eq(data)
end
end Now what would a declaration look like for an array here? I suppose require 'spec_helper'
describe Grape::Endpoint do
subject { Class.new(Grape::API) }
def app
subject
end
before do
subject.format :json
subject.params type: Array do
requires :foo, type: String
end
subject.post do
params
end
end
let(:data) { ::Grape::Json.dump([{ foo: 'bar' }, { foo: 'baz' }]) }
it 'responds' do
post '/', data, 'CONTENT_TYPE' => 'application/json'
expect(last_response.status).to eq 201
expect(last_response.body).to eq(data)
end
end This doesn't work yet, but I have a failing spec and some first level changes in https://github.com/dblock/grape/tree/issue-1730, so if someone wants to take a stab at it that'd be great. There's also a problem with how to declare that an array of at least 1 foo is required here. Maybe subject.requires type: Array do
requires :foo, type: String
end subject.optional type: Array do
requires :foo, type: String
end |
I was wondering if the top level param could be a special case, e.g. |
I think We always knew that |
we are not afraid :) but otherwise, an additional interface should be provided to separate those |
I spent an hour on this and concluded that there're too many assumptions around params being a hash, including in Rack proper. It's easy to add the body with But is something that wraps before do
subject.format :json
subject.body type: Array do
requires :foo, type: String
end
subject.post do
body
end
end |
while I have no strong feelings about this, |
Cool thx @dm1try. The only caveat is that when body is a Also, I am not working on it, maybe @Mateus-Resende wants to take a stab? |
Stumble into the same issue today trying to implement The RFC describe a PATCH payload consisting of a stray Array or operations. Found no way to express this with Grape (along the fact that Swagger generation, just choke on tries like
It could also be nice to have like in |
I will start working on that now, should have a working PR soon. I agree with @dblock that there are too many assumptions that
And this would allow
How does that sound? |
perhaps an addition to the DSL could be to add a Then from that named 'rewrap' we could count on the fact that all assumptions and validations are based on a This also would help swagger generation as the way to have both an form-encoded or a JSON payload in swagger is fro add this |
Lets not go crazy, keep things simple and lets look at some code ;) |
no crazy, no fun ;-) |
Hi there,
I'm trying to setup a route that will be able to receive a post request with an array (valid json) without having to name it. For example if I do
curl -XPOST -d '[{"foo": "baz"}, {"foo": "bar"}]' locahost:3000/foo
I want to be able to access this array of data. Is there any way I can do it ?
The text was updated successfully, but these errors were encountered: