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();
    }
}
相关推荐
AIbase20242 分钟前
国内MCP服务平台推荐!aibase.cn上线MCP服务器集合平台
运维·服务器·人工智能
艾立泰智能包装11 分钟前
电商分拣的“效率密码”:艾立泰轻量化托盘引领自动化流水线革新
运维·自动化
网安小白的进阶之路14 分钟前
A模块 系统与网络安全 第三门课 网络通信原理-3
网络·windows·安全·web安全·系统安全
cpsvps_net14 分钟前
Windows内存泄漏自动化
运维·自动化
喜欢吃豆42 分钟前
快速手搓一个MCP服务指南(九): FastMCP 服务器组合技术:构建模块化AI应用的终极方案
服务器·人工智能·python·深度学习·大模型·github·fastmcp
你不知道我是谁?1 小时前
负载均衡--常见负载均衡算法
运维·负载均衡
会又不会1 小时前
Jenkins-Publish HTML reports插件
运维·jenkins
烟雨书信1 小时前
Docker文件操作、数据卷、挂载
运维·docker·容器
IT成长日记1 小时前
【Docker基础】Docker数据卷管理:docker volume prune及其参数详解
运维·docker·容器·volume·prune
这儿有一堆花2 小时前
Docker编译环境搭建与开发实战指南
运维·docker·容器