繁体   English   中英

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

[英]Ruby Net::HTTP.post_form add data to headers

我试图弄清楚如何在使用使用Ruby Net :: HTTP的post_form时将数据添加到标头中

我在stackoverflow上看到了几篇文章, 在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)

我可以使用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)

暂无
暂无

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

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