通过curl针对REST服务器发送的HTTP POST请求会丢失JSON参数 [英] HTTP POST request sent via curl against REST server looses JSON parameters

查看:340
本文介绍了通过curl针对REST服务器发送的HTTP POST请求会丢失JSON参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有Rails的REST服务器,用于管理用户和关联的注释

这是路由配置。

I created a REST server with Rails that manages Users and associated Comments.
Here is the routes configuration.

resources :users do
  resources :comments
end

在控制器中,我只需要查询和创建注释的操作。交换格式为JSON。

In the controller I only need actions to query and create Comments. The exchange format is JSON.

class CommentsController < ApplicationController

  def index
    @user = User.find(params[:user_id])
    @comments = @user.comments  
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @comments }
    end
  end

  def create
    @user = User.find(params[:user_id])
    @comment = @user.comments.create!(params[:comment])
    redirect_to @user
  end

end

我想存储由创建的评论 远程客户端。这是一个Android应用程序。要测试服务器,我正在尝试以下 curl 命令,如此处所示。

I would like to store Comments created by a remote client. It is an Android application. To test the server I am trying the following curl commands, as suggested here.

curl -X POST -d @comment1.json http://localhost:3000/users/42/comments
curl -X POST -d @comment1.json http://localhost:3000/users/42/comments.json
curl -X POST -d @comment2.json http://localhost:3000/users/42/comments
curl -X POST -d @comment2.json http://localhost:3000/users/42/comments.json

我也不确定JSON文件的外观。以下是我尝试的变体:

comment1.json

I am also not sure how the JSON file has to look like. Here are the variations I tried:
comment1.json

{
  content:
  {
    message: "Let's see if this works.",
    subject: "JSON via curl"
  }
}

...或 comment2.json

{
  message: "Let's see if this works.",
  subject: "JSON via curl"
}

当我检查特定用户上的评论时,我可以看到然而,它已被创建,参数 subject 消息,我通过了,迷路了!

When I check the Comments on the particular User I can see that it has been created, however, the parameters subject and message, I passed, get lost somewhere!

[
  {
    created_at: "2012-08-11T20:00:00Z",
    id: 6,
    message: "null",
    subject: "null",
    updated_at: "2012-08-11T20:00:00Z",
    user_id: 42
  }
]

Rails安装包括以下宝石。

The Rails installation includes the following gems.

...
Using multi_json (1.3.6) 
Using json (1.7.4)
...

问题:


  • 如何通过以下方式测试评论的创建 curl 或使用任何其他合适的工具?

  • How can I test the creation of Comments via curl or with any other suitable tool?

推荐答案

尝试设置带有 -HContent-Type:application / json content-type 标头。
我认为Rails正在寻找作为表单数据的帖子参数(例如 content [subject] ='JSON via curl')。

Try setting the content-type header with -H "Content-Type:application/json". I think Rails is looking for the post parameters as form data (eg. content[subject]='JSON via curl').

此外,JSON文件无效。还需要引用JSON密钥。使用以下文件......

Further, the JSON file is not valid. JSON keys need to be quoted, too. Use the following file ...

{
  "message": "Let's see if this works.",
  "subject": "JSON via curl"
}

并发送其中一个命令...

and send it with one of these commands ...

curl -X POST -H "Content-Type:application/json" -d @comments2.json http://localhost:3000/users/42/comments
curl -X POST -H "Content-Type:application/json" -d @comments2.json http://localhost:3000/users/42/comments.json

这篇关于通过curl针对REST服务器发送的HTTP POST请求会丢失JSON参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆