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();
    }
}
相关推荐
saynaihe31 分钟前
2025吐槽季第一弹---腾讯云EO边缘安全加速平台服务
运维·安全·云计算·腾讯云
@#---39 分钟前
删除驱动精灵的详细过程
运维·服务器
boy快快长大1 小时前
【Elasticsearch】同一台服务器部署集群
服务器·elasticsearch·jenkins
likeyou~coucou1 小时前
自动化之ansible(二)
运维·自动化·ansible
小红帽2.01 小时前
客服系统自动化方案:揭秘全渠道智能服务解决方案 vx: haotsh
运维·自动化
~kiss~1 小时前
python的thrift2pyi学习
windows·python·学习
reset20211 小时前
ubuntu离线安装ollama
linux·ubuntu·ollama
放氮气的蜗牛2 小时前
Linux命令终极指南:从入门到精通掌握150+核心指令
linux·运维·服务器
网络安全King2 小时前
devops 工具 网络安全
运维·web安全·devops
梁萌2 小时前
04-DevOps-安装并初始化Jenkins
运维·jenkins·devops