简体   繁体   English

如何正确发送测试Rails的JSON请求?

[英]How to send correctly a JSON request testing Rails?

I'm witnessing an odd behavior in Rails, when sending a post request, with the following body: 发送以下内容时,我在Rails中目睹了奇怪的行为: 在此处输入图片说明

If you check is a Hash (converted to JSON when sending). 如果检查是否为哈希(发送时转换为JSON)。 But ODDLY when being read by params in controller, is recognized like this: 但是奇怪的是,当被控制器中的参数读取时,会被识别为:

在此处输入图片说明

If you check carefully, the attributes of :first_name and :email are moved to the previous item in the array. 如果仔细检查, :first_name:email的属性将移至数组中的上一项。

I'd thought if you have an array with certain attributes on the first item, but some different attributes on the following items, the array would be respecting the positions of which the attributes are set for. 我想如果您在第一个项目上有一个具有某些属性的数组,但在接下来的项目上有一些不同的属性,则该数组将尊重为其设置属性的位置。

I know most likely the answer would be "just put a nil or empty value of those attributes on the first item of the array" , but I'm more interested in the reason of why this is happening. 我知道答案很可能是“仅将这些属性的nil或空值放在数组的第一项上” ,但我对发生这种情况的原因更感兴趣。

Thank you. 谢谢。

UPDATE 更新

Thanks to a question, I replicated the scenario form a browser (I was originally doing the request from the test feature of rails), and checking the Network from the browser, this is what is being sent: 多亏了一个问题,我从浏览器复制了场景(我最初是从rails的test功能发出请求的),然后从浏览器检查网络,这就是发送的内容:

{
  "name": "un nombre",
  "team_members": [
    {
      "user_id": 1,
      "team_member_role_id": 4
    },
    {
      "email": "cpamerica@avengers.com",
      "first_name": "Cap America",
      "team_member_role_id": 4,
      "admin": true
    },
    {
      "email": "hulk@avenrgers.com",
      "first_name": "Bruce Banner",
      "team_member_role_id": 1,
      "admin": false
    },
    {
      "email": "ironman@avengers.com",
      "first_name": "Tony Stark",
      "team_member_role_id": 1,
      "admin": false
    },
    {
      "email": "thor@avengers.com",
      "first_name": "Thor Hijo de Odín",
      "team_member_role_id": 2,
      "admin": false
    }
  ]
}

And it works. 而且有效。 So I focused on how I was sending the info from the test environment, this is the actual code: 因此,我专注于如何从测试环境发送信息,这是实际的代码:

team        = {
    :name         => 'un nombre',
    :team_members => [
        {
            :user_id             => 1,
            :team_member_role_id => TeamMemberRole.role_communicator_id
        },
        {
            :email               => 'cpamerica@avengers.com',
            :first_name          => 'Cap America',
            :team_member_role_id => TeamMemberRole.role_communicator_id,
            :admin               => true
        },
        {
            :email               => 'hulk@avenrgers.com',
            :first_name          => 'Bruce Banner',
            :team_member_role_id => TeamMemberRole.role_visionary_id,
            :admin               => false
        },
        {
            :email               => 'ironman@avengers.com',
            :first_name          => 'Tony Stark',
            :team_member_role_id => TeamMemberRole.role_visionary_id,
            :admin               => false
        },
        {
            :email               => 'thor@avengers.com',
            :first_name          => 'Thor Hijo de Odín',
            :team_member_role_id => TeamMemberRole.role_developer_id,
            :admin               => false
        }
    ]
}
post create_team_path, :team => team, :format => :json

And what is being read in the controller by request.raw , this is what is getting (using the debugger): 以及request.raw在控制器中读取的内容(使用调试器)得到的是:

team[name]=un+nombre&
team[team_members][][user_id]=1&
team[team_members][][team_member_role_id]=4&
team[team_members][][email]=cpamerica%40avengers.com&
team[team_members][][first_name]=Cap+America&
team[team_members][][team_member_role_id]=4&
team[team_members][][admin]=true&
team[team_members][][email]=hulk%40avenrgers.com&
team[team_members][][first_name]=Bruce+Banner&
team[team_members][][team_member_role_id]=1&
team[team_members][][admin]=false&
team[team_members][][email]=ironman%40avengers.com&
team[team_members][][first_name]=Tony+Stark&
team[team_members][][team_member_role_id]=1&
team[team_members][][admin]=false&
team[team_members][][email]=thor%40avengers.com&
team[team_members][][first_name]=Thor+Hijo+de+Od%C3%ADn&
team[team_members][][team_member_role_id]=2&
team[team_members][][admin]=false&
format=json

Any idea on why the index of each team_member is missing? 关于为什么每个team_member的索引丢失的任何想法吗? Am I sending the array wrong? 我发送阵列错误吗?

To send an acceptable json request you need to send as a formed String instead the hash, also add the headers your code must look something like this: 要发送可接受的json请求,您需要以格式字符串(而不是哈希)形式发送,还添加标头,您的代码必须类似于以下内容:

post create_team_path, {:team => team}.to_json, 'CONTENT_TYPE' => 'application/json;charset=utf-8'

Note the problem is within the parser hash to form-request not adding the index of each object 请注意,问题出在解析器哈希中,它不向表单请求添加每个对象的索引

Source: How to post JSON data in rails 3 functional test 来源: 如何在Rails 3功能测试中发布JSON数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM