マルチパート JSON を Android から Rails 3.2 JSON API サーバーに POST しようとしています。
私はこのように MuttipartEntity を構築しています:
MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mp.addPart("DATA", new StringBody(jsonPostObject.toString()));
ここで、jsonPostObject は、投稿したいデータを含む JSONObject です。MultipartEntity の代わりに StringEntity を使用すると、すべて正常に動作しますが、ファイルと JSON ハッシュをアップロードする必要があります。
Rails コードは次のようになります。
@post = @current_user.posts.new(params[:DATA][:post])
次のエラーが表示されます。
[e0497f16bbc4bfacf4bdff43113f1d34] [127.0.0.1]
MultiJson::DecodeError (756: unexpected token at '--1Cg_oM4E624kPv1bobRRbZvkyO_kAKN3E
Content-Disposition: form-data; name="DATA"
{"tags":["df"],"location_id":2,"post":{"content":"test #df"}}
--1Cg_oM4E624kPv1bobRRbZvkyO_kAKN3E--
'):
json (1.6.6) lib/json/common.rb:148:in `parse'
json (1.6.6) lib/json/common.rb:148:in `parse'
multi_json (1.2.0) lib/multi_json/engines/json_common.rb:9:in `decode'
multi_json (1.2.0) lib/multi_json.rb:81:in `decode'
activesupport (3.2.3) lib/active_support/json/decoding.rb:12:in `decode'
actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:47:in ` parse_formatted_parameters'
actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.3) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:467:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__1468729498116700342__call__1422066066276649474__callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:14:in `block in call'
activesupport (3.2.3) lib/active_support/tagged_logging.rb:25:in `tagged'
railties (3.2.3) lib/rails/rack/logger.rb:14:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/gal/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/gal/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/gal/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
[e0497f16bbc4bfacf4bdff43113f1d34] [127.0.0.1] Rendered /home/gal/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.2ms)
[e0497f16bbc4bfacf4bdff43113f1d34] [127.0.0.1] Rendered /home/gal/.rvm/gems/ruby-1.9.3-p0/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.1ms)
[e0497f16bbc4bfacf4bdff43113f1d34] [127.0.0.1] Rendered /home/gal/.rvm/gems/ruby- 1.9.3-p0/gems/actionpack- 3.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.0ms)
アップデート:
これは「tcpflow」でキャプチャされたリクエストです:
POST /api/posts.json HTTP/1.1
Content-Type: application/json
Accepts: application/json
X-API-KEY: 7d867d16a5e25337b6d7857965f812bee73b76ac
Content-Length: 203
Host: 10.0.2.2:3000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
--UcMcwIxqCnaZ5GiqW1GVqb9JnK7n3rjH
Content-Disposition: form-data; name="DATA"
{"tags":["test"],"location_id":2,"post":{"content":"test content\n\n#test tag"}}
--UcMcwIxqCnaZ5GiqW1GVqb9JnK7n3rjH--
そのトークンを削除することは可能ですか、それともレールで適切にデコードするにはどうすればよいですか?