How to use curl to get a GET request exactly same as using Chrome?

前端 未结 3 806
北海茫月
北海茫月 2020-12-07 12:57

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.co         


        
相关标签:
3条回答
  • 2020-12-07 13:41

    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 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36" http://stackoverflow.com/questions/28760694/how-to-use-curl-to-get-a-get-request-exactly-same-as-using-chrome
    

    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
    
    0 讨论(0)
  • 2020-12-07 13:42

    Check the HTTP headers that chrome is sending with the request (Using browser extension or proxy) then try sending the same headers with CURL - Possibly one at a time till you figure out which header(s) makes the request work.

    curl -A [user-agent] -H [headers] "http://something.com/api"

    0 讨论(0)
  • 2020-12-07 13:58

    Open Chrome Developer Tools, go to Network tab, make your request (you may need to check "Preserve Log" if the page refreshes). Find the request on the left, right-click, "Copy as cURL".

    0 讨论(0)
提交回复
热议问题