簡體   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