简体   繁体   中英

Ruby on Rails rest-client gem send array params wrong

We have a form that will catch an X amount of inputs so we used arrays for setting up the info.

When we print the params looks like:

{
  "utf8" => "✓", "authenticity_token" => "DfpQDLOJBuc7ViR3XfY3iaK3/E9Hy9uQBWF8fwSkig0nchqj62DptkGunHOrH5bd79J0L+kG0P758cDqLJx7PQ==", "user" => "1", "Base" => {
    "cliente" => "1", "retencion" => "14", "anio" => "2018", "mes_inicio" => "9", "mes_fin" => "9", "serie" => "1", "nacionalidad" => "Nacional", "montoTotOperacion" => "11111.1", "montoTotGrav" => "11111.1", "montoTotExent" => "0.0", "montoTotRet" => "1111.1"
  }, "Dividendos" => [{
    "CveTipDivOUtil" => "01",
    "MontISRAcredRetMexico" => "0.0",
    "MontISRAcredRetExtranjero" => "0.0",
    "MontRetExtDivExt" => "0.0",
    "TipoSocDistrDiv" => "Sociedad Nacional",
    "MontISRAcredNal" => "0.0",
    "MontDivAcumNal" => "0.0",
    "MontDivAcumExt" => "0.0"
  }]
}

As you see the object "Dividendos" can be mutiple arrays, so we send the data to our API (Laravel Lumen).

    response = RestClient.post ENV["URL_API"]+'/createCFDI', { user: current_user.id, Dividendos: params[:Dividendos] }, {content_type: :json, accept: :json}

The main problem is that Rails send the reponse array of "Dividendos" like string.

array(2) {
  ["user"]=>
  string(1) "1"
  ["Dividendos"]=>
  array(1) {
    [0]=>
    string(235) "{"CveTipDivOUtil"=>"01", "MontISRAcredRetMexico"=>"0.0", "MontISRAcredRetExtranjero"=>"0.0", "MontRetExtDivExt"=>"0.0", "TipoSocDistrDiv"=>"Sociedad Nacional", "MontISRAcredNal"=>"0.0", "MontDivAcumNal"=>"0.0", "MontDivAcumExt"=>"0.0"}"
  }
}

Why all the data is ok, but only on nested array send it like string?

Are we missing something?

The issue is that params[:Dividendos] returns a String.

You should parse it to a hash and then, RestClient will parse it again to a JSON

Dividendos: JSON.parse(params[:Dividendos])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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