2016-11-17 24 views
0

我試圖弄清楚如何將數據添加到使用Ruby淨使用post_form時頭:: HTTP的Ruby的Net :: HTTP.post_form數據添加到頁眉

我看到幾個帖子在計算器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) 

回答

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) 
來解決這個