Ruby Net :: HTTP.post_form将数据添加到标头

时间:2016-11-17 01:03:15

标签: ruby-on-rails ruby net-http

我正在尝试弄清楚如何使用Ruby Net :: HTTP

使用post_form时向头文件添加数据

我在stackoverflow Add headers to a request in rails上看了几篇帖子 我浏览了https://github.com/augustl/net-http-cheat-sheet/blob/master/headers.rb

但是,我看不出怎么做。这是我的代码

headers = {
            'ApiKey' => org_user.api_key,
            'Authorization' => "Bearer #{x['access_token']}",
            'Content-Type' => 'application/x-www-form-urlencoded'
          }
      # Send data 
      params = {"ContactName": org_user.first_name,
                "MiddleName": nil,
                "LastName": org_user.last_name,
                "PrefixId": nil,
                "SuffixId": nil,
                "ContactUniqueId": nil,
                "ContactSourceId": nil,
                "Email": org_user.email,
                "Fax": nil,
                "Phone": nil,
                "AddressLine1": nil,
                "AddressLine2": nil,
                "AddressLine3": nil,
                "AddressLine4": nil,
                "City": nil,
                "State": nil,
                "Zip": nil,
                "County": nil,
                "Country": nil,
                "Id": nil
                }

        x = Net::HTTP.post_form(URI.parse('http://www.cloudapp.net/admin/contact/create/quick/individual'), params)

1 个答案:

答案 0 :(得分:0)

我能够通过使用post方法解决这个问题

headers = {
                'ApiKey' => org_user.api_key,
                'Authorization' => "Bearer #{access_token}",
                'Content-Type' => 'application/json'
              }


          params = {'ContactName' => user.first_name,
                    'MiddleName' => nil,
                    'LastName' => user.last_name,
                    'PrefixId' => nil,
                    'SuffixId' => nil,
                    'ContactUniqueId' => nil,
                    'ContactSourceId' => nil,
                    'Email' => user.email,
                    'Fax' => nil,
                    'Phone' => nil,
                    'AddressLine1' => nil,
                    'AddressLine2' => nil,
                    'AddressLine3' => nil,
                    'AddressLine4' => nil,
                    'City' => nil,
                    'State' => nil,
                    'Zip' => nil,
                    'County' => nil,
                    'Country' => nil,
                    'Id' => user.id

                    }

            url = URI.parse("www.somedomain.com/admin/contact/create/quick/individual")

            http = Net::HTTP.new(url.host, url.port)

            resp = http.post(url.path, params.to_json, headers)