将curl命令一键转换为java,python,go代码
如何将curl的命令转换为代码,首先打开浏览器的developer tools,先获取一个curl命令
如图所示,在network里面选择一个请求,并copy as curl
然后打开https://curlconverter.com/ 页面
将curl命令复制进去
curl命令:
curl 'https://blog.iamdev.cn/' \
-H 'authority: blog.iamdev.cn' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'accept-language: zh,en;q=0.9,zh-CN;q=0.8,ja;q=0.7,zh-TW;q=0.6' \
-H 'cache-control: max-age=0' \
-H 'cookie: _ga=GA1.1.937104759.1661133120; _ga_0WQ77K1FMC=GS1.1.1661224452.5.1.1661224453.59.0.0' \
-H 'dnt: 1' \
-H 'if-modified-since: Mon, 22 Aug 2022 14:22:43 GMT' \
-H 'sec-ch-ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "Windows"' \
-H 'sec-fetch-dest: document' \
-H 'sec-fetch-mode: navigate' \
-H 'sec-fetch-site: none' \
-H 'sec-fetch-user: ?1' \
-H 'upgrade-insecure-requests: 1' \
-H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' \
--compressed
转换的python代码如下:
import requests
cookies = {
'_ga': 'GA1.1.937104759.1661133120',
'_ga_0WQ77K1FMC': 'GS1.1.1661224452.5.1.1661224453.59.0.0',
}
headers = {
'authority': 'blog.iamdev.cn',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-language': 'zh,en;q=0.9,zh-CN;q=0.8,ja;q=0.7,zh-TW;q=0.6',
'cache-control': 'max-age=0',
# Requests sorts cookies= alphabetically
# 'cookie': '_ga=GA1.1.937104759.1661133120; _ga_0WQ77K1FMC=GS1.1.1661224452.5.1.1661224453.59.0.0',
'dnt': '1',
'if-modified-since': 'Mon, 22 Aug 2022 14:22:43 GMT',
'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}
response = requests.get('https://blog.iamdev.cn/', cookies=cookies, headers=headers)
转换的go代码如下:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://blog.iamdev.cn/", nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("authority", "blog.iamdev.cn")
req.Header.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
req.Header.Set("accept-language", "zh,en;q=0.9,zh-CN;q=0.8,ja;q=0.7,zh-TW;q=0.6")
req.Header.Set("cache-control", "max-age=0")
req.Header.Set("cookie", "_ga=GA1.1.937104759.1661133120; _ga_0WQ77K1FMC=GS1.1.1661224452.5.1.1661224453.59.0.0")
req.Header.Set("dnt", "1")
req.Header.Set("if-modified-since", "Mon, 22 Aug 2022 14:22:43 GMT")
req.Header.Set("sec-ch-ua", `".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
req.Header.Set("sec-fetch-dest", "document")
req.Header.Set("sec-fetch-mode", "navigate")
req.Header.Set("sec-fetch-site", "none")
req.Header.Set("sec-fetch-user", "?1")
req.Header.Set("upgrade-insecure-requests", "1")
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", bodyText)
}
转换的java代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
class Main {
public static void main(String[] args) throws IOException {
URL url = new URL("https://blog.iamdev.cn/");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("authority", "blog.iamdev.cn");
httpConn.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
httpConn.setRequestProperty("accept-language", "zh,en;q=0.9,zh-CN;q=0.8,ja;q=0.7,zh-TW;q=0.6");
httpConn.setRequestProperty("cache-control", "max-age=0");
httpConn.setRequestProperty("cookie", "_ga=GA1.1.937104759.1661133120; _ga_0WQ77K1FMC=GS1.1.1661224452.5.1.1661224453.59.0.0");
httpConn.setRequestProperty("dnt", "1");
httpConn.setRequestProperty("if-modified-since", "Mon, 22 Aug 2022 14:22:43 GMT");
httpConn.setRequestProperty("sec-ch-ua", "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"");
httpConn.setRequestProperty("sec-ch-ua-mobile", "?0");
httpConn.setRequestProperty("sec-ch-ua-platform", "\"Windows\"");
httpConn.setRequestProperty("sec-fetch-dest", "document");
httpConn.setRequestProperty("sec-fetch-mode", "navigate");
httpConn.setRequestProperty("sec-fetch-site", "none");
httpConn.setRequestProperty("sec-fetch-user", "?1");
httpConn.setRequestProperty("upgrade-insecure-requests", "1");
httpConn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
}
}
- 原文作者:iamdev
- 原文链接:https://blog.iamdev.cn/post/2022/curl-to-code/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止转载 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。