繁体   English   中英

如何在 Ruby on Rails 中将嵌套的 JSON 发布到 HTTParty

[英]How to post nested JSON to HTTParty in Ruby on Rails

我正在尝试找出使用 HTTParty 将嵌套的 JSON 对象发布到 API 的正确方法。

我使用 Postman 测试呼叫获得成功响应:

POST: http://service.net/api : http://service.net/api

标头: x-api-key : apikey123

身体 :

{
    "VehicleRequests": [{
        "Id": "Vehicle1",
        "Parameters": {
            "Term": 60,
            "CashDeposit": 10,
            "DepositType": "Percentage",
            "AnnualMileage": 10000
        },
        "PhysicalVehicle": {
            "ExternalVehicleId": "12345",
            "Type": "Car",
            "Status": "PreOwned",
            "OnTheRoadPrice": "30000",
            "Mileage": "12345",
            "Registration": {
                "RegistrationNumber": "REGN0",
                "DateRegisteredWithDvla": "01/01/2018"
            }
        }
    }]
}

这将返回:

{
  "Vehicles": [
    {
      "Id": "Vehicle1",
      "HasError": false,
      "Error": null,
      "FinanceQuotations": [
        {
          "HasError": false,
          "Error": null,
          "Finance": {
            "Key": "HP",
            "Notifications": [],
            "Quote": {
             .....
            }
          }
        }
    }
  ]
}

但我正在努力从我的 rails 应用程序复制调用。 我有一个课程设置,我正在调用创建

class Monthlyprice
   def initialize()

        @response = HTTParty.post('http://service.net/api',  
        :body =>{
            :VehicleRequests=> [{
                :Id => "Vehicle1",
                :Parameters => {
                    :Term => 60,
                    :CashDeposit => 10,
                    :DepositType => "Percentage",
                    :AnnualMileage => 10000
                },
                :PhysicalVehicle => {
                    :ExternalVehicleId => "12345",
                    :Type => "Car",
                    :Status => "PreOwned",
                    :OnTheRoadPrice => "30000",
                    :Mileage => "12345",
                    :Registration => {
                        :RegistrationNumber => "REGN0",
                        :DateRegisteredWithDvla => "01/01/2018"
                    }
                }
            }].to_json
        },
        :headers => {"x-api-key" => "apikey123"})

        puts(@response)
    end
end

但这会从 API 返回以下错误消息:

{"Error"=>{"UserMessage"=>"Request is invalid.", "TechnicalMessage"=>"Request Validation failed. Request had 2 error(s). 1: request.VehicleRequests[0].Id - The Id field is required.\r\n2: request.VehicleRequests[0].Parameters - The Parameters field is required.", "Code"=>"80000"}}

如果删除 Id 和 Parameters 对象,这表明我的 VehicleRequests 对象的内容格式不正确,这与我从 postman 中的 api 得到的错误相同? 任何建议都会很棒!

您能否更改如下语法:-

:body => {
         }.to_json

这意味着你必须使用.to_json正文括号关闭我认为这只是语法错误。

语法:-

response = HTTParty.post("your request URL",
                  headers: {
                    .....
                    #your header content
                    .....
                  },
                  body: {
                  ......
                   #your body content
                  .....

                   }.to_json
                 )

我刚刚编辑了您的代码,请尝试以下代码:-

@response = HTTParty.post('http://service.net/api',
        :headers => {"x-api-key" => "apikey123"},  
        :body =>{
            :VehicleRequests=> [{
                :Id => "Vehicle1",
                :Parameters => {
                    :Term => 60,
                    :CashDeposit => 10,
                    :DepositType => "Percentage",
                    :AnnualMileage => 10000
                },
                :PhysicalVehicle => {
                    :ExternalVehicleId => "12345",
                    :Type => "Car",
                    :Status => "PreOwned",
                    :OnTheRoadPrice => "30000",
                    :Mileage => "12345",
                    :Registration => {
                        :RegistrationNumber => "REGN0",
                        :DateRegisteredWithDvla => "01/01/2018"
                    }
                }
            }]
        }.to_json
        )

希望能帮到你 :)

暂无
暂无

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

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