❶ 如何POST一個jsON格式的數據給Restful服務
在Android/java平台上實現POST一個json數據:
JSONObject jsonObj = new JSONObject();
jsonObj.put("username", username);
jsonObj.put("apikey", apikey);
// Create the POST object and add the parameters
HttpPost 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);
❷ java中rest介面實現數據接收與轉發
java中的介面是一種特來殊的類,使用關源鍵字interface創建。介面功能完全實現後,可以打成jar包,提供給其他公司使用。
要返回json格式數據,可以把介面中抽象方法的返回值類型規定為JSONObject或JSONString類型。這樣當其他公司調用時,得到的數據就是json數據了。
另外,以jar形式提供的介面,可以通過反編譯得到你的源碼,如果你不希望開源,就要加密了。
❸ Ajax調用restful介面傳送Json格式數據的方法
ajax傳送json格式數據,關鍵是指定contentType,data要是json格式
如果是restful介面,把type改成對應的post(增)、delete(刪)、put(改)、get(查)即可
var
post_data={"name":"test001","pass":"xxxx"};
$.ajax({
url:
"http://192.168.10.111:8080/uc/login",
type:
'post',
contentType:
"application/json;
charset=utf-8",
data:JSON.stringify(post_data),
success:function
(data)
{
//調用成功
},
error:
function(data,
textStatus,
errorThrown){
//調用失敗
}
});
以上所述是小編給大家介紹的Ajax調用restful介面傳送Json格式數據的方法的全部敘述,希望對大家有所幫助,如果大家想了解更多內容敬請關注腳本之家網站!
❹ RESTClient到底該怎麼提交JSON對象啊啊啊
很多REST Client是不支持自動化測試 API,也不支持自動生成API文檔.
之前習慣用一款名字為 WisdomTool REST Client,支持自動化測試RESTful API,輸出精美的測試報告,並且自動生成RESTful API文檔。
工具精悍哦!
https://github.com/wisdomtool/rest-client
Most of REST Client tools do not support automated testing.
Once used a tool called WisdomTool REST Client supports automated testing, output exquisite report, and automatically generating RESTful API document.
Very powerful !
https://github.com/wisdomtool/rest-client
❺ REST WebService 怎麼響應JSON格式數據
在ajax 定義返回數據類型, dataType: 'json', 在webservice中返回json數據格式,webservice默認應該返回HMTL格式。 望採納 Thx
❻ get傳遞json串,spring restful怎麼接收
在Android/java平台上實現POST一個json數據:JSONObjectjsonObj
❼ restful Post提交 輸入參數是json格式,怎麼調用
解決方法:
static async void Create()
{
var employee = new Employee { Department = "研發部內", Grade = "G8", Id = "003", Name = "王五容" };
var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.PostAsJsonAsync<Employee>("IP/employees/zlb", employee);
var reply = response.Content.ReadAsAsync<Employee>().Result;
Console.WriteLine(reply);
}
❽ java內部怎麼訪問rest獲取json數據
不訪問這個rest介面,而是和這個rest介面一樣訪問相應的數據
~~~~~~~~~~~~~~~~
❾ cxf restful json請求對象怎麼接收
接收json,請求數據比較簡單,在服務端方法參數,定義一個相應的含json鍵的實體,添加專jackson的一個相應屬provider,例如com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider,即可
❿ 使用rest,後台怎樣返回json數據,struts2以上框架,用的是註解
struts2中用rest後台返回json的方法是統一封裝response為JSONObject即可。
舉例如下:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.Action;
public class Test {
public Map responseJson;
public Map getResponseJson() {
return responseJson;
}
public void setResponseJson(Map responseJson) {
this.responseJson = responseJson;
}
public String getList(){
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for(int i=0;i<3;i++){
Map<String, Object> m = new HashMap<String, Object>();
m.put("id", i);
m.put("name", "Mic"+i);
list.add(m);
}
map.put("rows", list);
map.put("totalCont", 3);
this.setResponseJson(map);
return Action.SUCCESS;
}
}