ruby,python及curl post请求
#飘红部分为变量
test_url="http://test"
body_hash={"value"=>100, "year"=>2014, "month"=>11, "day"=>12, "hour"=>16, "minute"=>9, "second"=>0, "host"=>"test"}
body_json=body_hash.to_json
#for curl
curl -X POST -H "Content-Type:application/json" --connect-timeout 10 -m 20 -d '$body_json' $test_url
#for ruby
def send_data(test_url,body_hash)
data=body_hash
data=data.to_json
puts "data for json is #{data}"
url = URI.parse(test_url)
req = Net::HTTP::Post.new(url.path,{'Content-Type' => 'application/json'})
req.body = data
begin
res = Net::HTTP.new(url.host,url.port).start do |http|
http.read_timeout=5
http.request(req)
end
rescue Exception
res = ''
puts "error:#{$!} at:#{$@}!"
puts "post to dest failed!"
return 1
end
post_res=res.body
puts "post result is #{post_res}"
end
=================================================
#sms发送短信:
#网关给的api请求key,使用 HTTP Basic Auth 方式进行身份验证,使用api作为验证用户名,API key是验证密码
api-key=key-14a206a26676b946e8df722axxxxxxxx
#网关请求api地址
api-url="https://sms-api.xxx.com/v1/send.json"
#短信签名,短信签名指附加在短信内容末尾的署名信息,使用发送者的公司名或产品名,格式为:【XXXXX】
api-name="【测试】"
#for curl
curl -s -k --user api:$api-key $api-url -F mobile='12345678901' -F message='Send message for test! $api-name'
#for python
mob="12345678901"
msg="for test"+api-name
def send_messag(mob,msg):
print mob
print msg
resp = requests.post((api-url),
auth=("api", api-key),
data={
"mobile": mob,
"message": msg
},timeout=3 , verify=False);
result = json.loads( resp.content )
print result
#for php,未测试
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$api-url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD , 'api:$api-key');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => '12345678901','message' => 'test【测试】'));
$res = curl_exec( $ch );
curl_close( $ch );
//$res = curl_error( $ch );
var_dump($res);
================ruby uri====================
require 'uri'
uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413")
#=> #<URI::HTTP:0x00000000b14880
URL:http://foo.com/posts?id=30&limit=5#time=1305298413>
uri.scheme
#=> "http"
uri.host
#=> "foo.com"
uri.path
#=> "/posts"
uri.port
#=> "80"
uri.query
#=> "id=30&limit=5" uri.fragment #=> "time=1305298413"
uri.to_s
#=> "http://foo.com/posts?id=30&limit=5#time=1305298413"
ruby,python及curl post请求的更多相关文章
- 【转】提交http请求之python与curl
提交http请求之python与curl 由于Openstack是python实现wsgi的REST ful架构,在学习和调试的过程中,常常会遇到http请求的提交,于是顺手整理下python和cur ...
- python学习--curl
PyCurl是一个C语言写的libcurl的python绑定库.libcurl 是一个自由的,并且容易使用的用在客户端的 URL 传输库.它的功能很强大,PycURL 是一个非常快速(参考多并发操作) ...
- python 3 处理HTTP 请求的包
http http: https://docs.python.org/3/library/http.html http是一个包,里面含有多个模块:http.client,http.server,htt ...
- python CSRF跨站请求伪造
python CSRF跨站请求伪造 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- curl get请求添加header头信息
function get($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPGET, true); curl_setopt($ch, CU ...
- linux c++ curl https 请求并双向验证SSL证书
1.配置curl https请求需要提供 CA证书.客户端证书和客户端秘钥,这三个文件的pem格式. 分别对应 curl_easy_setopt() 函数的 下面三个参数: CURLOPT_CAINF ...
- python发送HTTP POST请求
1. 127.0.0.1和0.0.0.0 127.0.0.1是一个回送地址,指本地机,一般用来本机测试使用,使用127.0.0.1启的服务只能在本地机器上访问,使用0.0.0.0启的服务可以在其他机器 ...
- 转:PHP中的使用curl发送请求(GET请求和POST请求)
原文地址:http://www.jb51.net/article/104974.htm 使用CURL发送请求的基本流程 使用CURL的PHP扩展完成一个HTTP请求的发送一般有以下几个步骤: 1.初始 ...
- python error: curl: (1) Protocol "'https" not supported or disabled in libcurl
python 调用curl访问一个网页时,出现error: curl: (1) Protocol "'https" not supported or disabled in lib ...
随机推荐
- PHP高级教程-JSON
PHP JSON 本章节我们将为大家介绍如何使用 PHP 语言来编码和解码 JSON 对象. 环境配置 在 php5.2.0 及以上版本已经内置 JSON 扩展. JSON 函数 函数 描述 json ...
- SAP SQ01怎样从开发机传输到生产机
确认你的Query是本地的还是全局的(跨Client).假设是后者,会自己主动生成传输请求,用标准传输方式就可以. 假设是本地的Query,有两种方式: 方式1:复制成全局的,让后生成传输请求 方式2 ...
- windows命令行设置IP与DNS
用dos命令修改IP等本地连接属性 平时我们改IP通常都在是窗口界面本地连接直接修改, 那在命令行也可以设置IP地址?当然可以,这里要用到netsh命令 .点击“开始”->“运行”,输入“cmd ...
- JqGrid获得所有选中行数据ID数组,获取所有行的ID数组
获得选中行的ID数组:var ids = $("jqgridtableid").jqGrid('getGridParam','selarrrow'); 获得所有行的ID数组:var ...
- component和bean区别
@Component and @Bean do two quite different things, and shouldn't be confused. @Component (and @Serv ...
- 新浪API及调用案例
百度分享:http://pan.baidu.com/s/1uSmku
- Job for vsftpd.service failed because the control process exited with error code
# systemctl start vsftpd.serviceJob for vsftpd.service failed because the control process exited wit ...
- Android 一个apk多个ICON执行入口
一个工程对应一个AndroidManifest.xml文件,这个文件中包含有该项目的一些设置,如权限.SDk版Activity.Service信息等.一般而言,这个文件中会有且仅有一个applicat ...
- 【微信小程序】:客服消息教程
1.本教程完全链接W3Cschool的教程,已经讲的非常清晰和透彻. 2.链接:https://www.w3cschool.cn/weixinapp/weixinapp-api-custommsg-c ...
- Spring Data JPA实体详解
1. Spring Data JPA实体概述 JPA提供了一种简单高效的方式来管理Java对象(POJO)到关系数据库的映射,此类Java对象称为JPA实体或简称实体.实体通常与底层数据库中的单个关系 ...