0

i have this http call code, the type is form

      param = {
      form: {
      "creatives[]" => [
        {
          is_visible: params[:creative_banner_is_visible],
          type: "banner",
          value_translations: {
              id: params[:creative_banner_value_id],
              en: params[:creative_banner_value_en]
          }
        },
        {
          is_visible: params[:creative_video_is_visible],
          type: "video",
          value_translations: {
              id: params[:creative_video_value_id],
              en: params[:creative_video_value_en]
          }
        }
      ]
      }
      }

      http = HTTP.headers(headers)
      http.put(base_url, param)

but somehow this is translated to this on the target server

"creatives"=>[
    "{:is_visible=>\"true\", :type=>\"banner\", :value_translations=>{:id=>\"Banner URL ID\", :en=>\"Banner URL EN\"}}",
    "{:is_visible=>\"true\", :type=>\"video\", :value_translations=>{:id=>\"12345ID\", :en=>\"12345EN\"}}"
]

do you know how to make this http call not stringified? i used same schema on postman and work just fine

    "creatives": [
        {
            "is_visible": true,
            "type": "banner",
            "value_translations": {
                "id": "http://schroeder.info/elinore",
                "en": "http://wehner.info/dusti"
            }
        },
        {
            "is_visible": true,
            "type": "video",
            "value_translations": {
                "id": "85177e87-6b53-4268-9a3c-b7f1c206e002",
                "en": "5134f3ca-ead7-4ab1-986f-a695e69ace96"
            }
        }
    ]

i'm using this gem https://github.com/httprb/http

2 Answers2

0

EDIT

First, replace your "creatives[]" => [ ... with creatives: [ ... so the end result should be the following.

      creatives = [
        {
          is_visible: params[:creative_banner_is_visible],
          type: "banner",
          value_translations: {
              id: params[:creative_banner_value_id],
              en: params[:creative_banner_value_en]
          }
        },
        {
          is_visible: params[:creative_video_is_visible],
          type: "video",
          value_translations: {
              id: params[:creative_video_value_id],
              en: params[:creative_video_value_en]
          }
        }
      ]

      http = HTTP.headers(headers)
      http.put(base_url, creatives.to_json)

Second, I don't see any problem with what you get in your target server, you just have to parse it to JSON, so if you also have a Rails app there use JSON.parse on the body.

Patricio Sard
  • 2,092
  • 3
  • 22
  • 52
  • I have tried this approach but then resulting in the array modified to a hash – Muhammad Umar Ramadhana Jul 22 '20 at 22:31
  • If you don't want a hash, then don't encapsulate `creatives` inside params. I updated my question and that should work now. – Patricio Sard Jul 22 '20 at 22:38
  • ``` http.post(base_url, param) NoMethodError: undefined method `to_hash' for # ``` getting that error, and to get more context, this have similar problem https://stackoverflow.com/questions/34313657/html-form-post-an-array-of-objects have tried that, but somehow it only send the last object of the array – Muhammad Umar Ramadhana Jul 22 '20 at 23:11
0

somehow this approach fixed the issue

    create_params = {}.compare_by_identity

    create_params["creatives[][is_visible]"] = params[:creative_banner_is_visible]
    create_params["creatives[][type]"] = 'banner'
    create_params["creatives[][value_translations][id]"] = params[:creative_banner_value_id]
    create_params["creatives[][value_translations][en]"] = params[:creative_banner_value_en]
    create_params["creatives[][is_visible]"] = params[:creative_video_is_visible]
    create_params["creatives[][type]"] = 'video'
    create_params["creatives[][value_translations][id]"] = params[:creative_video_value_id]
    create_params["creatives[][value_translations][en]"] = params[:creative_video_value_en]