如何POST一个JSON格式的数据给Restful服务
来源:互联网 发布:邮箱注册知乎怎么登陆 编辑:程序博客网 时间:2024/05/21 10:02
在Android/java平台上实现POST一个json数据:
JSONObject jsonObj = new JSONObject();jsonObj.put("username", username);jsonObj.put("apikey", apikey);// Create the POST object and add the parametersHttpPost httpPost = new HttpPost(url);StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);entity.setContentType("application/json");httpPost.setEntity(entity);HttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(httpPost);
用curl可执行如下命令:
curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://domain/apis/users.json
用jQuery:
$.ajax({ url:url, type:"POST", data:data, contentType:"application/json; charset=utf-8", dataType:"json", success: function(){ ... }})
PHP用cUrl实现:
$data = array("name" => "Hagrid", "age" => "36"); $data_string = json_encode($data); $ch = curl_init('http://api.local/rest/users'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); $result = curl_exec($ch);
by iefreer
- 如何POST一个JSON格式的数据给Restful服务
- 如何POST一个JSON格式的数据给Restful服务
- 如何POST一个JSON格式的数据给Restful服务
- 如何POST一个JSON格式的数据给Restful服务
- 如何使用PHP调用API接口( 如何POST一个JSON格式的数据给Restful服务)
- 使用RESTful API以post+json的方式给服务器传数据
- POST xml文件给RESTful服务
- mui post json 格式数据
- AFNetworking 如何POST一个JSON数据到服务器
- AFNetworking 如何POST一个JSON数据到服务器
- afnetworking如何post一个json数据到服务器
- AFNetworking 如何POST一个JSON数据到服务器
- AFNetworking 如何POST一个JSON数据到服务器
- AFNetworking如何post一个json数据到服务器
- AFNetworking如何post一个json数据到服务器
- AFNetworking如何post一个json数据到服务器
- AFNetworking如何post一个json数据到服务器
- AFNetworking如何post一个json数据到服务器
- 三层架构与MVC
- apns服务器介绍
- c# 获取Access 表名称和字段
- Linux 下串口编程入门
- 正则表达式学习笔记
- 如何POST一个JSON格式的数据给Restful服务
- 定义一个结构体变量stu,成员包括学号,姓名,性别,成绩。
- 工作队列
- Linux Vi 常用命令
- 建立一个简单的链表,它由三个学生数据的结点组成输出各节点中的数据。
- 从云端盗取数据
- Sql复杂查询--多表联接
- 最新数据库相关技术整理
- 21点小游戏C++代码