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();
    }
}
相关推荐
CoolTiger、1 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
m0_741768851 小时前
使用docker的小例子
运维·docker·容器
学习3人组2 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神2 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu
Karoku0662 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
geek_Chen012 小时前
虚拟机共享文件夹开启后mnt/hgfs/下无sharefiles? --已解决
linux·运维·服务器
(⊙o⊙)~哦3 小时前
linux 解压缩
linux·运维·服务器
牧小七4 小时前
Linux命令---查看端口是否被占用
linux
IT学长编程4 小时前
计算机毕业设计 教师科研信息管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·教师科研管理系统
最新小梦4 小时前
Docker日志管理
运维·docker·容器