我有一个对 Rails JSON API 服务器的发布请求,如下所示:
POST /api/posts.json HTTP/1.1
Accepts: application/json
X-API-KEY: 7d867d16a5e25337b6d7857965f812bee73b76ac
Content-Length: 288
Content-Type: multipart/form-data; boundary=syoYQUQsGwI2XqShQimFdv2QSe-_GYbjVx40T1kS
Host: 10.0.2.2:3000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)
--syoYQUQsGwI2XqShQimFdv2QSe-_GYbjVx40T1kS
Content-Disposition: form-data; name="DATA"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit
{"tags":["dffdff"],"location_id":3,"post":{"content":"test #dffdff"}}
--syoYQUQsGwI2XqShQimFdv2QSe-_GYbjVx40T1kS--
如何访问 JSON 参数?
如果我将 params[:DATA] 写入日志,我会看到属性,但嵌套的所有内容都是空的(例如 params[:DATA][:post] 或 params[:DATA][:tags]
更新:
当我想做的时候
Rails.logger.debug(params[:DATA])
我明白了
{"tags":["dffdff"],"location_id":3,"post":{"content":"test #dffdff"}}
但是当我尝试
Rails.logger.debug(params[:DATA][:post])
我明白了
TypeError (can't convert Symbol into Integer):
app/controllers/api/posts_controller.rb:19:in `[]'
app/controllers/api/posts_controller.rb:19:in `create'