如何在Rails中执行Get/Post/Put请求

require 'open-uri'
require 'json'
require 'net/http'
class CoupleController < ApplicationController
  skip_before_filter :verify_authenticity_token, :only => [:raw_data_put]

  def raw_date_get
    #如果有GET请求参数直接写在URI地址中
    begin
      uri = 'http://qa.services.theknot.com/registry/v1/couples/2170045'
      html_response = nil
      open(uri) do |http|
        html_response = http.read
      end
      data = JSON.parse(html_response)
      registries=data["CoupleRegistries"]
      render json: data.has_key?("Id")
    rescue (excetpion)
      render json: excetpion
    end
  end

  def raw_data_put
    require 'net/http'
    begin
      retailerRegistry=Hash.new()
      retailerRegistry["RetailerId"]=12160
      retailerRegistry["RetailerRegistryCode"]="cjq2I5Wc3u_zIU_QFkvy0w"
      retailerRegistry["RegistrantFirstName"]="Kelly"
      retailerRegistry["RegistrantLastName"]="Bu"
      retailerRegistry["RegistrantEmail"]="zzkelly.burch.890@gmail.com"
      retailerRegistry["CoRegistrantFirstName"]="Zacuu"
      retailerRegistry["CoRegistrantLastName"]="Wilson"
      retailerRegistry["CoRegistrantEmail"]=""
      retailerRegistry["EventDate"]="2014-11-15"
      retailerRegistry["EventTypeId"]=1
      retailerRegistry["IsDeleted"]=false
      retailerRegistry["EventDescription"]=""
      retailerRegistry["ReferralStatusCode"]=""
      retailerRegistry["EventTypeId"]=1
      retailerRegistry["City"]=""
      retailerRegistry["State"]="GA"
      retailerRegistry["Zip"]=""
      retailerRegistry["Country"]=""
      retailerRegistry["AltRetailerRegistryCode"]="TargetUCfnNSDSM"
      #port = 80
      host = 'qa.services.theknot.com'
      path = '/registry/v1/raw/retailerRegistry'

      req = Net::HTTP::Put.new(path, initheader = {'Content-Type' => 'application/json'})
      req.body = retailerRegistry.to_json
      response = Net::HTTP.new(host).start { |http| http.request(req) }
      puts response.code
      render json: response.code
    rescue Exception => e
      puts e.message
      puts e.backtrace.inspect
      render json: e
      #render json: '{"result":"failed"}'
    end
  end

  def raw_data_post
    begin
      affiliate=Hash.new()
      affiliate["Id"]=2
      affiliate["Name"]="Test0003"
      affiliate["AffiliateCode"]="Test0003"
      affiliate["AffiliateUrl"]="Test0003"
      affiliate["Description"]="Test0003"
      host = 'qa.services.theknot.com'
      path = '/registry/v1/affiliates'
      req = Net::HTTP::Post.new(path, initheader = {'Content-Type' => 'application/json'})
      req.body = affiliate.to_json
      response = Net::HTTP.new(host).start { |http| http.request(req) }
      puts response.code
      render json: response.body
    rescue Exception => e
      puts e.message
      puts e.backtrace.inspect
      render json: e.message
    end
  end
 end

 

Routes:

 get 'couple/raw_data_get'
  put 'couple/raw_data_put'
  get 'couple/raw_data_post'

转载于:https://www.cnblogs.com/mawenzhu/p/3929067.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值