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'

如何在Rails中执行Get/Post/Put请求的更多相关文章

  1. Fiddler抓取Intellij Idea中执行的web网络请求

    首先可以打开命令行 输入:ipconfig 找到本机配置的IP地址 这里是: 192.168.97.122 或者打开Fiddler 点击如下图片中的小三角符号:将鼠标放在online的位置,也可以看到 ...

  2. 【转】如何在 Windows 中执行干净启动

    完成故障排除后,请执行以下步骤将计算机重置为正常启动. Windows 8.1 和 Windows 8 从屏幕右边缘滑入,然后点按“搜索”.您也可以将鼠标指向屏幕的右下角,然后单击“搜索”. 在搜索框 ...

  3. 如何在 Rails 中搭配 Turbolinks 使用 Vue

    [Rails] Vue-outlet for Turbolinks 在踩了 Rails + Turbolinks + Vue 的許多坑後,整理  的作法並和大家分享. Initialize the A ...

  4. Dockfile中的命令如何在.sh中执行

    有类似如下内容的Dokefile文件.1 RUN cd /tmp/patch \ && /lib/python3./site-packages/moduleA/a.* \ && ...

  5. 如何在cmd中执行python文件

    打开cmd终端 输入python     然后再输入要执行文件的路径 就可以把python文件运行起来                                                  ...

  6. 关于如何在MVC中 执行JS

    Response.Write("<script>KHTPREFERENCE()</script>"); return this.MessageResult( ...

  7. 如何在java中发起http和https请求

    一般调用外部接口会需要用到http和https请求. 一.发起http请求 1.写http请求方法 //处理http请求 requestUrl为请求地址 requestMethod请求方式,值为&qu ...

  8. 如何在Java中调用Python代码

    有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...

  9. phpStorm如何在Console控制台执行php文本,而不是浏览器中

    如何在Console控制台执行php文本 phpStorm默认会在浏览器中执行脚本 默认的配置 配置PHP脚本 扩展,配置项目运行

随机推荐

  1. EF框架step by step(1)—Database-First

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,现已经包含在 Visual Studio 2008 S ...

  2. 【原】iOS学习之ARC和非ARC文件混编

    在编程过程中,我们会用到很多各种各样的他人封装的第三方代码,但是有很多第三方都是在非ARC情况下运行的,当你使用第三方编译时出现和下图类似的错误,就说明该第三方是非ARC的,需要进行一些配置.

  3. Leetcode Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. HDU 4020 Ads Proposal

    题目链接 排排序,找找做题的感觉... 对了,longlong用C++ 错了几次,我也是linux选手了.... #include <iostream> #include <cstd ...

  5. 【Oracle】同义词问题

    优点: 1.节省数据库空间,多用户可以操作同一张表: 2.扩展的数据库的使用范围,能够在不同的数据库用户之间实现无缝交互: 3.利用Database Link.创建同义词可以实现不同数据库服务器之间的 ...

  6. jquery dialog open后,服务器端控件失效的快速解决方法

    jquery dialog为我们提供了非常漂亮实用的对话框,比单调的alert.confirm.prompt好用很多. 在使用jquery与.net共同开发时,直接调用jquery dialog的op ...

  7. [VBA] 打开文件夹

    '显示打开文件夹对话框 With Application.FileDialog(msoFileDialogFolderPicker) .Show Then Exit Sub '未选择文件夹 strFo ...

  8. Qt Write and Read XML File 读写XML文件

    在Qt中,我们有时候需要把一些参数写入xml文件,方便以后可以读入,类似一种存档读档的操作,例如,我们想生成如下的xml文件: <?xml version="1.0" enc ...

  9. print_r、echo、var_dump三者的区别

    这三者均是php里输出的方式. 1.print_r用于输出数组. 2.echo只能用于输出字符串,在没有框架的时候,多用这种方法将结果打印在html页面上. 3.var_dump能输出任何类型的数据, ...

  10. thinkphp上传至服务器后模板无法解析原因

    前几日做好的响应式静态页面上传至虚拟空间,打开网址地址出现: 模板不存在:./app/Home/View/Index/index.html 错误位置 FILE: /home/u333385714/pu ...