curl在window及linux中的使用及区别

目录

内容介绍

测试一(GET,application/json)

测试二(GET,x-www-form-urlencoded)

测试三(POST,FORM-DATA)

测试四(POST,x-www-form-urlencoded)

总结


内容介绍

注:通过实际测试,摆出在linux环境与windows环境下系统使用curl的不同之处
注:测试工具:ApiFox、cmd命令行、git bash命令行、idea spring boot web服务端
注:测试使用的curl可在这里生成


注:测试url。包含请求头、请求体、请求类型、请求url、换行符

一、测试一(GET,application/json)

测试内容:GET请求, json请求体, 带请求头

shell 复制代码
# linux 环境使用
curl --location --request GET 'http://localhost:8090/test/curltest/getRequest' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name":"i am aliens"
}'
# windows环境使用
curl --location --request GET "http://localhost:8090/test/curltest/getRequest" ^
--header "User-Agent: Apifox/1.0.0 (https://apifox.com)" ^
--header "Content-Type: application/json" ^
--data-raw "{\"name\":\"i am aliens\"}"

注:请求后返回值
git bash

cmd

归纳

注:字符的处理

key word 举例 windows linux
url http://localhost:8090/test/curltest/getRequest 使用了双引号包裹 使用了单引号包裹
换行符 ^ 反斜杠(\) 上标(^)
请求体 data-raw 除包裹字符串使用双引号外,json中key和value的双引号还需要使用反斜杠进行转义 包裹字符串使用单引号, json内部双引号不用处理

二、测试二(GET,x-www-form-urlencoded)

测试内容:GET请求,form请求参数(Content-Type: x-www-form-urlencoded), 带请求头

shell 复制代码
# linux环境
curl --location --request GET "http://localhost:8090/test/curltest/getRequest2?who=aliens" ^
--header "User-Agent: Apifox/1.0.0 (https://apifox.com)" ^
--header "Content-Type: x-www-form-urlencoded"
# windows环境
curl --location --request GET 'http://localhost:8090/test/curltest/getRequest2?who=aliens' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: x-www-form-urlencoded'

git bash

cmd

归纳

注:字符的处理

key word 举例 windows linux
url?paran=value http://localhost:8090/test/curltest/getRequest2?who=aliens 没有区别 没有区别
url http://localhost:8090/test/curltest/getRequest 使用了双引号包裹 使用了单引号包裹
换行符 ^ 反斜杠(\) 上标(^)

三、测试三(POST,FORM-DATA)

测试内容:POST请求,form请求参数(Content-Type: form-data), 带请求头

shell 复制代码
# linux环境
curl --location --request POST 'http://localhost:8090/test/curltest/getRequest3' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--form 'who="aliens"'
# windows环境
curl --location --request POST "http://localhost:8090/test/curltest/getRequest3" ^
--header "User-Agent: Apifox/1.0.0 (https://apifox.com)" ^
--form "who=\"aliens\""

git bash

cmd

归纳

注:字符的处理

key word 举例 windows linux
url http://localhost:8090/test/curltest/getRequest 使用了双引号包裹 使用了单引号包裹
换行符 ^ 反斜杠(\) 上标(^)
请求体 form-data 除包裹字符串使用双引号外,key和value的双引号还需要使用反斜杠进行转义 包裹字符串使用单引号, key值不做处理, value值使用双引号

四、测试四(POST,x-www-form-urlencoded)

测试内容:POST请求,form请求参数(Content-Type: form-data), 带请求头

shell 复制代码
# linux环境
curl --location --request POST 'http://localhost:8090/test/curltest/getRequest3' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--data-urlencode 'who=aliens'
# windows环境
curl --location --request POST "http://localhost:8090/test/curltest/getRequest3" ^
--header "User-Agent: Apifox/1.0.0 (https://apifox.com)" ^
--data-urlencode "who=aliens"

git bash
cmd

归纳

注:字符的处理

key word 举例 windows linux
url http://localhost:8090/test/curltest/getRequest 使用了双引号包裹 使用了单引号包裹
换行符 ^ 反斜杠(\) 上标(^)
请求体 x-www-form-urlencoded 没有区别 没有区别

总结

1. 汇总
请求类型 key word 举例 windows linux
* url http://localhost:8090/test/curltest/getRequest 使用了双引号包裹 使用了单引号包裹
* 换行符 ^ 反斜杠(\) 上标(^)
* 请求体 data-raw(json) 除包裹字符串使用双引号外,json中key和value的双引号还需要使用反斜杠进行转义 包裹字符串使用单引号, json内部双引号不用处理
GET 请求体 url?paran=value(url上的参数) 没有区别 没有区别
POST 请求体 x-www-form-urlencoded(编码的参数) 没有区别 没有区别
2. 学会了解不同

注:如果使用apifox生成业务代码curl命令行的方式可以解决在不同系统之间使用的目的。但为了方便我们自己在修改参数时可以灵活运用不同系统之间的特性,了解他们之间的区别也很重要。知己知彼,百用不怠

3. 服务端代码
java 复制代码
package com.home.api;

import com.home.entity.RequestDTO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("curltest")
public class CurlTestController {

    @GetMapping("getRequest")
    public ResponseEntity getRequest(HttpServletRequest request
            , @RequestBody RequestDTO requestDTO
                                     , @RequestHeader("User-Agent") String userAgent
    ) throws InterruptedException {
        System.out.println("requestDTO: "+requestDTO.toString());
        System.out.println("userAgent: "+userAgent);
        return ResponseEntity.success();
    }

    @GetMapping("getRequest2")
    public ResponseEntity getRequest2(HttpServletRequest request
            , @RequestHeader("User-Agent") String userAgent
            , @RequestParam("who") String who
    ) throws InterruptedException {
        System.out.println("userAgent: "+userAgent);
        System.out.println("who: "+who);
        return ResponseEntity.success();
    }
	 /** form-data, x-www-form-urlencoded都可以使用 */
    @PostMapping("getRequest3")
    public ResponseEntity getRequest3(HttpServletRequest request
            , @RequestHeader("User-Agent") String userAgent
            , @RequestParam("who") String who
    ) throws InterruptedException {
        System.out.println("userAgent: "+userAgent);
        System.out.println("who: "+who);
        return ResponseEntity.success();
    }
}
相关推荐
CQU_JIAKE14 分钟前
3.24[Q]Linux
linux·运维·服务器
hgdlip21 分钟前
两个手机IP地址一样说明什么?
服务器·tcp/ip·智能手机
Apache IoTDB25 分钟前
IoTDB 常见问题 Q&A 第六期
linux·运维·服务器·数据库·iotdb
m0_7482500340 分钟前
【AIDevops】Deepseek驱动无界面自动化运维与分布式脚本系统,初探运维革命之路
运维·分布式·自动化
zyx没烦恼43 分钟前
Linux 进程间通信
linux·运维·服务器
永不复还44 分钟前
穿透Session 0隔离
windows
re1ife1 小时前
32位汇编:MASM32环境搭建与汇编窗口程序
汇编·c++·windows·开源软件
武汉唯众智创1 小时前
物联网系统部署与运维实训室
运维·物联网·物联网系统部署与运维实训室·物联网系统部署与运维实验室·物联网系统部署与运维·物联网部署与运维
zhangzeyuaaa1 小时前
Linux 挂载磁盘操作指南
linux·运维·服务器
wanhengidc2 小时前
视频网站服务器网络连接不稳定该如何解决?
运维·服务器·网络·小程序