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();
    }
}
相关推荐
电鱼智能的电小鱼7 分钟前
基于 EFISH-SBC-RK3588 的无人机通信云端数据处理模块方案‌
linux·网络·人工智能·嵌入式硬件·无人机·边缘计算
星霜旅人22 分钟前
【Linux】Vim文本编辑器
linux
難釋懷1 小时前
Shell脚本-for循环应用案例
linux·运维·服务器·bash
何双新1 小时前
L3-3、从单轮到链式任务:设计协作型 Prompt 系统
服务器·搜索引擎·prompt
昊昊昊昊昊明1 小时前
10天学会嵌入式技术之51单片机-day-7
linux·运维·网络
Sonetto19991 小时前
Nginx 反向代理,啥是“反向代理“啊,为啥叫“反向“代理?而不叫“正向”代理?它能干哈?
运维·前端·nginx
江沉晚呤时1 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
达斯维达的大眼睛1 小时前
如何在Linux用libevent写一个聊天服务器
linux·运维·服务器·网络
末央&1 小时前
【Linux】gdb工具,Linux 下程序调试的 “透视眼”
linux·运维·服务器
绵绵细雨中的乡音1 小时前
Linux进程学习【进程状态】
linux