【问题标题】:Ruby Net::HTTP.post_form add data to headersRuby Net::HTTP.post_form 将数据添加到标头
【发布时间】:2016-11-17 13:21:37
【问题描述】:

我试图弄清楚如何在使用 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)

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 感谢您回答自己的问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多