解析 cURL(bash) 命令
- 1. 主要用于解析从浏览器复制来的 cURL(bash)
- 2. 废话不多说,都在🍻代码里了。
- 参考资料
1. 主要用于解析从浏览器复制来的 cURL(bash)
curl 'https://eva2.csdn.net/v3/06981375190026432f77c01bfca33e32/lts/groups/dadde766-b087-42da-8e67-d2499a520ee7/streams/a0119567-bf91-4314-ab75-f683ba6c0c0a/logs' \-H 'authority: eva2.csdn.net' \-H 'accept: */*' \-H 'accept-language: zh-CN,zh;q=0.9' \-H 'cache-control: no-cache' \-H 'content-type: application/json' \-H 'lts-sdk-request-id: 6c6b1e6bc1b5482fad39fd89e1be7e04' \-H 'lts-sdk-version: 1.0.15' \-H 'origin: https://so.csdn.net' \-H 'pragma: no-cache' \-H 'referer: https://so.csdn.net/so/search?q=java&t=blog&u=jx520' \-H 'sec-ch-ua: "Not-A.Brand";v="24", "Chromium";v="14"' \-H 'sec-ch-ua-mobile: ?0' \-H 'sec-ch-ua-platform: "Windows"' \-H 'sec-fetch-dest: empty' \-H 'sec-fetch-mode: cors' \-H 'sec-fetch-site: same-site' \-H 'user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36' \-H 'x-sdk-date: 20231014T001112Z' \--data-raw '{"labels":{},"logs":[{"contents":[{"pid":"so","ref":"https://blog.csdn.net/jx520/?type=blog","curl":"https://so.csdn.net/so/search?q=java&t=blog&u=jx520","spm":"1018.2226","disabled":"true","extra":"{\"x\":763,\"y\":72,\"ev\":\"move\"}","tos":3,"adb":0,"cCookie":"c_ins_um=-;c_ins_fref=-;c_ins_fpage=/;c_ins_prid=1695268703975_658854;c_ins_rid=1695735575959_588639;c_dl_prid=1696211031774_715058;c_dl_rid=1696416086259_366423;c_dl_fref=https://blog.csdn.net/m0_46365130/article/details/122106794;c_dl_fpage=/download/z18813048784/10697308;c_dl_um=-;c_utm_source=wwwtab;c_session_id=10_1697241559533.454757;c_segment=5;c_sid=1c05424b88433722b9b201b4d2024577;c_first_ref=default;c_utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-114031899-blog-109717731.235%5Ev38%5Epc_relevant_default_base;c_utm_relevant_index=2;c_pref=default;c_first_page=https%3A//blog.csdn.net/jx520/;c_dsid=11_1697242179555.095819;c_page_id=default;c_ref=https%3A//blog.csdn.net/jx520/%3Ftype%3Dblog;","t":1697242273,"screen":"1920*1080","urn":"1697242263003-f4828940-1336-41c0-8a4d-ba464f4a65e1","vType":"U010000","log_id":"56","sign":"7b1e61a11f5c4647a7f7fad1c85bdc70","userAgent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.125 Safari/537.36","cid":"10_18489767550-1694529713132-118469","uid":"jx520","sid":"10_1697241559533.454757","dc_sid":"1c05424b88433722b9b201b4d2024577","did":"10_18489767550-1694529713132-118469","utm":"","un":"jx520","__client_time__":1697242272521}]}]}' \--compressed
2. 废话不多说,都在🍻代码里了。
package com.jerry.utils;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;import static java.util.stream.Collectors.groupingBy;/*** 解析 cURL(bash) 命令*/
@Slf4j
public class CUrlUtil {/*** 解析 curl 命令** @param curlCmd curl 命令* @return*/public static CurlEntity parserCurl(String curlCmd) {String[] cmdParams; // curl 命令按行切成的数组String url; // 请求地址Map<String, String> headers; // 请求头 mapString body; // 请求体// 把 curl 命令按行切成数组cmdParams = curlCmd.split("\n");// 取请求地址url = StrUtil.trimByChar(cmdParams[0], "'");// log.info("url = {}", url);// 将请求头、请求体分开Map<String, List<String>> headerAndBody = Stream.of(cmdParams).collect(groupingBy((String str) -> str.trim().startsWith("-H") ? "header" : "body"));// 提取请求头 mapheaders = headerAndBody.get("header").stream().map(str -> StrUtil.trimByChar(str, "'").split(": ")).collect(Collectors.toMap(strArr -> strArr[0], strArr -> strArr[1]));// headers.put("Content-Encoding","gzip"); // compress gzip 补上压缩参数。让服务器如果支持就返回压缩结果// log.info("headers = {}", headers);// 提取请求体body = headerAndBody.get("body").stream().filter(str -> str.trim().startsWith("--data-raw")).map(str -> StrUtil.trimByChar(str, "'", "' \\")).collect(Collectors.joining());// log.info("body = {}", body);// 返回一个 curl 实体对象。包含:请求地址、请求头、请求体return new CurlEntity(url, headers, body);}@Data@AllArgsConstructorpublic static class CurlEntity {String url; // 请求地址Map<String, String> headers; // 请求头 mapString body; // 请求体}
}
参考资料
curl 命令在线执行
在线:curl 命令转成其他语言