cURL does not work but same site works with browser

血红的双手。 提交于 2021-02-06 03:43:50

问题


what are the reasons for when using cURL form linux server to access the site return an error. but opening this site in chrome succeeds.


回答1:


Look browsers headers while sending your request, and add same headers to cURL request. Some servers needs some headers that browsers sends default but not cURL.




回答2:


To avoid/suppress SSL error, use -k flag, ie

curl -k ...



回答3:


I had 301 Moved Permanently error on curl but was working on browser.

But when I looked deeper there's Location to moved to location, which would be working. This coulde be when the server is using TLS.

$ curl -v http://www.shaharma.com/location/v1/US/zipcode/98104
*   Trying 172.111.99.100...
* TCP_NODELAY set
* Connected to www.shaharma.com (172.111.99.100) port 80 (#0)
> GET /location/v1/US/zipcode/98104 HTTP/1.1
> Host: www.shaharma.com
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Content-Length: 0
< Location: https://www.shaharma.com/location/v1/US/zipcode/98104
< Cache-Control: max-age=0
< Expires: Fri, 01 Dec 2017 19:40:14 GMT
< Date: Fri, 01 Dec 2017 19:40:14 GMT
< Connection: Keep-Alive
< Set-Cookie: UID=52679eee-c06c-49fc-893a-69fd7e46bad0; expires=Fri, 25-Feb-2028 19:40:14 GMT; path=/; domain=.shaharma.com
< Set-Cookie: SID=1ada5fa9-0ace-4f4e-b75b-7a756b8da934; path=/; domain=.shaharma.com
< Set-Cookie: shaharma_loc_lb=p-loc-w; expires=Fri, 01-Dec-2017 19:50:14 GMT; path=/; domain=.shaharma.com
< Set-Cookie: bby_rdp=l; expires=Sat, 02-Dec-2017 19:40:14 GMT; path=/; domain=.shaharma.com
< 
* Connection #0 to host www.shaharma.com left intact

Also, you can goto brower and see the network which will have headers information.



来源:https://stackoverflow.com/questions/38160420/curl-does-not-work-but-same-site-works-with-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!