curl post request with -H "Content-type: application/json" causing params not to appear

759 views
Skip to first unread message

Joran Kikke

unread,
Feb 28, 2013, 7:59:26 AM2/28/13
to espresso-...@googlegroups.com
Hey, thanks for all the help so far, stuck on a new thing!

I'm trying to post JSON to one of our APIs running Espresso.

If I make this request:

curl -X POST -d ' {"item":{"text":"I LIKE CAKE", "tags":[2840]}, "auth_token" : "koz3EX1hAEi3JEwuV9L9"}' http://localhost:3001/items

Espresso generates a request like this as I'd expect:

(Inside Item controller)
#<EspressoRequest:0x007fc172c534e8 @env={"SERVER_SOFTWARE"=>"thin 1.5.0 codename Knife", "SERVER_NAME"=>"localhost", "rack.input"=>#<StringIO:0x007fc172c54208>, "rack.version"=>[1, 0], "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>false, "rack.multiprocess"=>false, "rack.run_once"=>false, "REQUEST_METHOD"=>"POST", "REQUEST_PATH"=>"/items", "PATH_INFO"=>"", "REQUEST_URI"=>"/items", "HTTP_VERSION"=>"HTTP/1.1", "HTTP_USER_AGENT"=>"curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5", "HTTP_HOST"=>"localhost:3001", "HTTP_ACCEPT"=>"*/*", "CONTENT_LENGTH"=>"90", "CONTENT_TYPE"=>"application/x-www-form-urlencoded", "GATEWAY_INTERFACE"=>"CGI/1.2", "SERVER_PORT"=>"3001", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "rack.url_scheme"=>"http", "SCRIPT_NAME"=>"/items", "REMOTE_ADDR"=>"127.0.0.1", "async.callback"=>#<Method: Thin::Connection#post_process>, "async.close"=>#<EventMachine::DefaultDeferrable:0x007fc172c538a8>, "espresso.path_info"=>nil, "espresso.format"=>nil, "rack.request.query_string"=>"", "rack.request.query_hash"=>{}, "rack.request.form_input"=>#<StringIO:0x007fc172c54208>, "rack.request.form_hash"=>{"json"=>"{\"item\":{\"text\":\"I LIKE CAKE\", \"tags\":[2840]}, \"auth_token\" : \"koz3EX1hAEi3JEwuV9L9\"}"}, "rack.request.form_vars"=>"json={\"item\":{\"text\":\"I LIKE CAKE\", \"tags\":[2840]}, \"auth_token\" : \"koz3EX1hAEi3JEwuV9L9\"}"}, @params={"json"=>"{\"item\":{\"text\":\"I LIKE CAKE\", \"tags\":[2840]}, \"auth_token\" : \"koz3EX1hAEi3JEwuV9L9\"}"}>


But if I make the same request, but set Content-type: application/json, it does not get the params through:

curl -H "Content-type: application/json" -X POST -d ' {"item":{"text":"I LIKE CAKE", "tags":[2840]}, "auth_token" : "koz3EX1hAEi3JEwuV9L9"}' http://localhost:3001/items

(Inside Item controller)
#<EspressoRequest:0x007fc172c05cc0 @env={"SERVER_SOFTWARE"=>"thin 1.5.0 codename Knife", "SERVER_NAME"=>"localhost", "rack.input"=>#<StringIO:0x007fc172c06a30>, "rack.version"=>[1, 0], "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>false, "rack.multiprocess"=>false, "rack.run_once"=>false, "REQUEST_METHOD"=>"POST", "REQUEST_PATH"=>"/items", "PATH_INFO"=>"", "REQUEST_URI"=>"/items", "HTTP_VERSION"=>"HTTP/1.1", "HTTP_USER_AGENT"=>"curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5", "HTTP_HOST"=>"localhost:3001", "HTTP_ACCEPT"=>"application/json", "CONTENT_LENGTH"=>"86", "CONTENT_TYPE"=>"application/json", "GATEWAY_INTERFACE"=>"CGI/1.2", "SERVER_PORT"=>"3001", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "rack.url_scheme"=>"http", "SCRIPT_NAME"=>"/items", "REMOTE_ADDR"=>"127.0.0.1", "async.callback"=>#<Method: Thin::Connection#post_process>, "async.close"=>#<EventMachine::DefaultDeferrable:0x007fc172c06288>, "espresso.path_info"=>nil, "espresso.format"=>nil, "rack.request.query_string"=>"", "rack.request.query_hash"=>{}}, @params={}>

How should I set this up?

Joran

Silviu Rusu

unread,
Feb 28, 2013, 8:25:38 AM2/28/13
to espresso-...@googlegroups.com
Content-Type is a Response(read server -> client) header, by which server informs client about the type of sent content.
If you need client to inform the server about accepted/expected content type, you should use Accept header, which is a Request(read client -> server) header.

try
-H "Accept: application/json"

then on Espresso end you can find out whether client accepts json like this:

if accept? /json/
  ...
end



--
You received this message because you are subscribed to the Google Groups "Espresso Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to espresso-framew...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Joran Kikke

unread,
Feb 28, 2013, 12:05:58 PM2/28/13
to espresso-...@googlegroups.com
Ah okay, thanks thats useful to know!
To unsubscribe from this group and stop receiving emails from it, send an email to espresso-framework+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages