如何使用 curl 获取与使用 Chrome 完全相同的 GET 请求? [英] How to use curl to get a GET request exactly same as using Chrome?

查看:49
本文介绍了如何使用 curl 获取与使用 Chrome 完全相同的 GET 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 web api http://something.com/api 并且我想使用 GET 来获取响应正文.

这是我的命令:

curl "http://something.com/api"

当然,它失败并给出错误信息.

当我使用 Chrome 并输入上述网址时,一切正常.但是我用 Firefox 做同样的事情,网址给了我同样的错误信息.我尝试使用 Chrome 扩展 DHC 重复该操作,该请求再次给出了正确的响应.经过一番搜索,我相信 curl 选项 --user-agent 会有所作为.将用户代理设置为 Chrome 的正确方法是什么?或者这不是重点,问题来自其他领域?非常感谢.

解决方案

如果需要在 curl 请求中设置用户头字符串,可以使用 -H 选项设置用户代理,如:

curl -H user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"http://stackoverflow.com/questions/28760694/how-to-use-curl-to-get-a-get-request-exactly-same-as-using-chrome

在 2021 年 2 月 22 日更新了最新版 Chrome 的用户代理


使用像

您现在有一个可以在终端中运行的 cURL 请求,该请求将反映您的浏览器发出的请求.这是我对此页面的请求(删除了 cookie 标头):

curl -H "Host: stackoverflow.com";-H "缓存控制: max-age=0";-H "接受: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";-H 用户代理:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36"-H "HTTPS: 1";-HDNT:1"-H 推荐人:https://www.google.com/"-H "Accept-Language: en-US,en;q=0.8,en-GB;q=0.6,es;q=0.4";-HIf-Modified-Since: Thu, 23 Jul 2015 20:31:28 GMT"--compressed http://stackoverflow.com/questions/28760694/how-to-use-curl-to-get-a-get-request-exactly-same-as-using-chrome

I have a web api http://something.com/api and I want to use GET to get the response body.

This is my command:

curl "http://something.com/api"

Of course, it fails and gives an error message.

When I use Chrome and input the above url, everythings correct. However I do the same things with Firefox, the url gives me the same error message. I try to repeat the action with Chrome extension DHC, the request gives correct response again. After some searching, I believe that the curl option --user-agent makes a difference. What is the correct way to set the user agent to Chrome? Or this is not the point, the problem comes from other fields? Thank you very much.

解决方案

If you need to set the user header string in the curl request, you can use the -H option to set user agent like:

curl -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36" http://stackoverflow.com/questions/28760694/how-to-use-curl-to-get-a-get-request-exactly-same-as-using-chrome

Updated user-agent form newest Chrome at 02-22-2021


Using a proxy tool like Charles Proxy really helps make short work of something like what you are asking. Here is what I do, using this SO page as an example (as of July 2015 using Charles version 3.10):

  1. Get Charles Proxy running
  2. Make web request using browser
  3. Find desired request in Charles Proxy
  4. Right click on request in Charles Proxy
  5. Select 'Copy cURL Request'

You now have a cURL request you can run in a terminal that will mirror the request your browser made. Here is what my request to this page looked like (with the cookie header removed):

curl -H "Host: stackoverflow.com" -H "Cache-Control: max-age=0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" -H "HTTPS: 1" -H "DNT: 1" -H "Referer: https://www.google.com/" -H "Accept-Language: en-US,en;q=0.8,en-GB;q=0.6,es;q=0.4" -H "If-Modified-Since: Thu, 23 Jul 2015 20:31:28 GMT" --compressed http://stackoverflow.com/questions/28760694/how-to-use-curl-to-get-a-get-request-exactly-same-as-using-chrome

这篇关于如何使用 curl 获取与使用 Chrome 完全相同的 GET 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆