1、curl使用简介
1.get
示例:localhost:3000/api/basic?name=kang
1.application/x-www-form-urlencoded
示例:curl localhost:3000/api/basic -X POST -d 'hello=world'
2.application/json
示例:curl localhost:3000/api/json -X POST -d '{"hello": "world"}' --header "Content-Type: application/json"
3.multipart/form-data
示例:curl localhost:3000/api/multipart -F raw=@raw.data -F hello=world
一、postman调用接口
1、get请求 – 选择params
1、Params 传参只能用于get请求
2、post请求 -- 选择body
1、none 不传参数
2、form-data 传递文件和参数
3、x-www-form-urlencoded 传递表单格式参数(后台接收格式为:name=kang&age=21)
4、row 格式(常用的为json)
1、json 参数以json格式传递(后台接收格式为:{name:kang,age:21})
二、jquery调用接口
1、get请求
1、参数直接拼接在地址后面即可。以?开头,每个参数间以&连接。(如:http://localhost:8080/Hello/demo?name=kang&age=21)
2、post请求 -- 默认采用表单提交(x-www-form-urlencoded)
1、表单方式提交
var name = "kang";
var age = 21;
dataType: "json",
data: {"name": name, "age": age} -- 会自动转为 name=kang,age=21 格式
2、json方式提交
var name = "kang";
var age = 21;
var obj = {"name": name, "age": age};
dataType: "json",
data: JSON.stringify(obj)
三、后台接收接口
1、获取 get post(表单方式)参数
public String demo(String name, int age) {
return “demo”;
}
2、获取post(json格式)参数
class User {
private String name;
private int age;
}
public String demo(@RequestBody User user) {
return "demo";
}
四、调用远程接口 HttpClient – 使用时工具类需设置为单例
1、get请求
@GetMapping(“fun1”)
@ResponseBody
public void fun1(String name, int age) throws IOException {
//接受接口响应结果
String respContent = null;
//创建get方式的请求地址
HttpGet httpGet = new HttpGet(“http://localhost:8080/user/get1?name=" + name + “&age=” + age);
//相当于打开浏览器
try(CloseableHttpClient client = HttpClients.createDefault()) {
//相当于在浏览器执行此请求地址
HttpResponse resp = client.execute(httpGet);
//获得相应状态码
if (resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
//获得远程接口返回的结果
respContent = EntityUtils.toString(he, “UTF-8”);
}
}catch (Exception e) {
System.out.println(“接口调用异常!!!”);
}
System.out.println(respContent);
}
2、post之表单格式
@PostMapping("fun2")
@ResponseBody
public void fun2(String name, int age) throws IOException {
//接受接口响应结果
String respContent = null;
//创建post方式的请求地址
HttpPost httpPost = new HttpPost("http://localhost:8080/user/post1");
//设置表单格式的请求参数
List<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("name", name));
list.add(new BasicNameValuePair("age", age + ""));
UrlEncodedFormEntity params = new UrlEncodedFormEntity(list, "UTF-8");
params.setContentEncoding("UTF-8");
params.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(params);
//相当于打开浏览器
try(CloseableHttpClient client = HttpClients.createDefault()) {
//相当于在浏览器执行此请求地址
HttpResponse resp = client.execute(httpPost);
//获得相应状态码,判断是否执行请求成功
if (resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
//获得远程接口返回的结果
respContent = EntityUtils.toString(he, "UTF-8");
}
}catch (Exception e) {
System.out.println("请求接口异常!!!");
}
System.out.println(respContent);
}
3、post之json格式
@PostMapping("fun3")
@ResponseBody
public void fun3(@RequestBody TUser user) throws IOException {
//接收接口响应结果
String result = "";
//创建post方式的请求地址
HttpPost httpPost = new HttpPost("http://localhost:8080/user/post2");
//设置json格式的请求参数
StringEntity entity = new StringEntity(JSON.toJSONString(user), "UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
//相当于打开浏览器
try(CloseableHttpClient client = HttpClients.createDefault()) {
//相当于在浏览器执行此请求地址
HttpResponse response = client.execute(httpPost);
//获得相应状态码,判断是否执行请求成功
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entitys = response.getEntity();
//获得远程接口返回的结果
result = EntityUtils.toString(entitys, "UTF-8");
}
}catch (Exception e) {
System.out.println("请求接口异常!!!");
}
System.out.println(result);
}
五、jsonp跨域请求(只能get请求)
1、get请求(默认为表单方式提交)
function getDatas() {
$.ajax({
url: “http://localhost:8083/fun1/datas",
//只能get请求
type: “GET”,
dataType: “JSONP”,
data: {
“name”: “kang”,
//此参数可以让后台判断是否使用的是jsonp方式请求的,若是则返回jsonp需要的格式,不是则直接返回restful格式的数据
“callback”: “success_jsonpCallback”
},
//指定回调为本请求的success方法
jsonpCallback: “success_jsonpCallback”,
//成功后的回调 – 后台返回格式需要为:return “success_jsonpCallback(“ + result + “)”;
success: function (res) {
console.log(“成功”);
console.log(res);
}
})
}