curl常用参数介绍

  • 什么是curl?
    curl是一种命令行工具,用于请求web服务器,与Postman这类工具相似,差别在于它是一个命令行工具,没有图形化展示。

  • 如果curl后面不带任何参数,则表示发送get请求
    $ curl http://www.baidu.com

返回结果:



<html>
	<head>
		 
		<meta content=always name=referrer>
		<link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css> <title>鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾title>
	head>
	<body link=#0000cc>
		<div id=wrapper>
			<div id=head>
				<div class=head_wrapper>
					<div class=s_form>
						<div class=s_form_wrapper>
							<div id=lg>
								<img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129>
							div>
							<form id=form name=f action=//www.baidu.com/s class=fm>
								<input type=hidden name=bdorz_come value=1>
								<input type=hidden name=ie value=utf-8>
								<input type=hidden name=f value=8>
								<input type=hidden name=rsv_bp value=1>
								<input type=hidden name=rsv_idx value=1>
								<input type=hidden name=tn value=baidu>
								<span class="bg s_ipt_wr">
									<input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus>
								span>
								<span class="bg s_btn_wr">
									
								span>
							form>
						div>
					div>
					<div id=u1>
						<a href=http://news.baidu.com name=tj_trnews class=mnav>鏂伴椈a>
						<a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123a>
						<a href=http://map.baidu.com name=tj_trmap class=mnav>鍦板浘a>
						<a href=http://v.baidu.com name=tj_trvideo class=mnav>瑙嗛a>
						<a href=http://tieba.baidu.com name=tj_trtieba class=mnav>璐村惂a>
						<noscript>
							&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>鐧诲綍a>
						noscript>
						<script>
							document.write('+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">鐧诲綍');
						script>
						<a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">鏇村浜у 搧a>
					div>
				div>
			div>
			<div id=ftCon>
				<div id=ftConw>
					<p id=lh>
						<a href=http://home.baidu.com>鍏充簬鐧惧害 a> <a href=http://ir.baidu.com>About Baidua> p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>浣跨敤鐧惧害鍓嶅繀璇?/a>  <a href=http://jianyi.baidu.com/ class=cp-feedback>鎰忚鍙嶉a> 浜琁CP璇?30173鍙?nbsp;
							<img src=//www.baidu.com/img/gs.gif> 
                    p>
                div> 
            div> 
        div> 
    body> 
html>
  • 如果想指定请求的方法,则可以使用 -X 参数后接请求方式名的方式
    $ curl -X POST http://www.sian.com
<html>
	<head>
		<title>301 Moved Permanentlytitle>
	head>
	<body bgcolor="white">
		<center>
			<h1>301 Moved Permanentlyh1>
		center>
		<hr>
		<center>nginx/1.8.0center>
	body>
html>

当然也可以指定PUT、DELETE等其他请求方式

  • 当想设置请求头里面的信息时,则可以配合 -H 参数来实现
    $ -H "Content-Type: application/json" 设置请求头的类型为json格式

  • 想在请求体里面传递数据,则需要使用 -d 参数配合使用
    $ curl -X POST http://www.baidu.com -d 'username=zhangshan&password=123'

上面的命令表示在请求体,传入username=zhangshan&password=123这个数据。没有指定请求体数据类型,默认为:Content-Type : application/x-www-form-urlencoded以表单的形式提交。

  • 如果想在请求体传入json格式的数据,则需要 -H -d配合使用
    $ curl -X POST http://www.baidu.com -H "Content-Type: application/json" -d "{\"username\":zhangshan, \"password\":123}

  • 总结
    curl是一个测试网络请求的工具,可以用来做后端接口测试
    可以使用java语言,生成一条curl命令,然后根据java的动态编译功能,执行curl命令,以达到自动化接口测试的功能。

java动态执行curl命令

public static String execCurl(String[] cmds) {
     

        ProcessBuilder processBuilder = new ProcessBuilder(cmds);
        Process process = null;

        try {
     
            process = processBuilder.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
     
                sb.append(line);
                sb.append(System.getProperty("line.separator"));
            }
            return sb.toString();

        } catch (IOException e) {
     
            System.out.println("error");
            e.printStackTrace();
        }

        return null;
    }

拼接curl命令字符串

  public static String[] generalCurlCmd(String url, String method, String token, String requestParam)   {
     
        
        String[] curl = new String[10];

        curl[0] = "curl";

        curl[1] = "-X";

        curl[2] = method;

        curl[3] = url;

        curl[4] = "-H";

        curl[5] = token;

        curl[6] = "-H";

        curl[7] = "Content-Type: application/json";

        curl[8] = "-d";

        curl[9] = requestParam;

        return curl;
    }

接口测试

public static void main(String[] args) {
     
        String[] cmds = {
     };
        
        Map<String, Object> requestParamMap = new HashMap<>();
        requestParamMap.put("reasonCode", "10");
        requestParamMap.put("contrabandTypeCode", "4");
        requestParamMap.put("isSequestered", true);
        requestParamMap.put("packageNos", "234234243");
        String requestParam = JackJsonUtil.obj2String(requestParamMap);

        String[] curlCmd = ExecCurl.generalCurlCmd("http://127.0.0.1:8088/app/weixin/send-back-packages", "POST", "Authentication-Token: RniFIai2Kb7iHF5bXOhBKy7zw3LbGhnNO/9N62kAa4nkPLIyjAT7Fp/jI+NWybKo", requestParam);
        System.out.println(ExecCurl.execCurl(curlCmd));
    }

你可能感兴趣的:(测试工具,curl常用命令,curl接口测试,java执行curl)