简体   繁体   English

即使状态代码是400,如何从curl请求输出到文件?

[英]how to get output from curl request to a file even if status code is 400?

if I do a request via curl to a normal site I may direct the output to a file with header data and stuff like this: 如果我通过curl向普通网站发出请求,我可以将输出定向到带有标题数据的文件,如下所示:

Command: 命令:

curl -is www.google.de > temp.txt

Output: 输出:

HTTP/1.1 200 OK
Date: Wed, 29 Feb 2012 16:49:09 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=16ad7441d475580e:FF=0:TM=1330534149:LM=1330534149:S=2MpGe04BWQ8Kn3P7; expires=Fri, 28-Feb-2014 16:49:09 GMT; path=/; domain=.google.de
Set-Cookie: NID=57=BCgUcqNQU5dMWcBEMP85saDNUdXg2_LG4eZFmS-yr1W38UyTNuB8BU8iiOOsK2747WGJG99uJDW6t8N78D4QzBma7q2EmKQ69-gCjOysFNLGFggrvMxcqEOl7p1hemeQ; expires=Thu, 30-Aug-2012 16:49:09 GMT; path=/; domain=.google.de; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked

<!doctype html><html itemscope itemtype="http://schema.org/WebPage"><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><meta itemprop="image" content="/images/google_favicon_128.png"><title>Google</title><script>window.google={kEI:"BVdOT7OzE8_ktQaqk-WGDw",getEI:function(a){var d;while(a&&!(a.getAttribute&&(d=a.getAttribute("eid"))))a=a.parentNode;return d||google.kEI},https:function(){return window.location.protocol=="https:"},kEXPI:"31701,33551,34324,34904,35055,35091,36604,36683,36716,36946,37017,37055,37126",kCSI:{e:"31701,33551,34324,34904,35055,35091,36604,36683,36716,36946,37017,37055,37126",ei:"BVdOT7OzE8_ktQaqk-WGDw"},authuser:0,
ml:function(){},kHL:"de",time:function(){return(new Date).getTime()},log:function(a,d,f,h){var e=new Image,g=google,k=g.lc,i=g.li,m="";e.onerror=(e.onload=(e.onabort=function(){delete k[i]}));k[i]=e;if(!f&&d.search("&ei=")==-1)m="&ei="+google.getEI(h);var j=f||"/gen_204?atyp=i&ct="+a+"&cad="+d+m+"&zx="+google.time(),b=/^http:/i;if(b.test(j)&&google.https()){google.ml(new Error("GLMM"),false,{src:j});
delete k[i];return}e.src=j;g.li=i...

When I do the same request to an url, where the response will have an status code 400, I will get an error that will be prompted to the shell and the file will be empty: 当我对一个url做同样的请求时,响应将有一个状态代码400,我将收到一个错误,提示shell,文件将为空:

curl -is https://foursquare.com/oauth2/access_token?grant_type=authorization_code&client_id=SOME_CLIENT_ID&client_secret=SOME_CLIENT_SECRET&code=notavalidcode&redirect_uri=SOME_REDIRECT_URI > temp.txt

Is there a way to prompt the result to a file also if I get an status code 400? 如果我获得状态代码400,是否有办法将结果提示给文件?

wrap the url with " "包裹”网址

kent$  curl -is "https://foursquare.com/oauth2/access_token?grant_type=authorization_code&client_id=SOME_CLIENT_ID&client_secret=SOME_CLIENT_SECRET&code=notavalidcode&redirect_uri=SOME_REDIRECT_URI" > temp.txt

kent$  cat temp.txt 
HTTP/1.0 200 Connection established


HTTP/1.1 400 Bad Request
Cache-Control: no-cache, private, no-store
Content-Type: application/json; charset=utf-8
Date: Wed, 29 Feb 2012 16:59:16 GMT
Expires: Wed, 29 Feb 2012 16:59:16 GMT
Pragma: no-cache
Server: nginx/0.8.52
Content-Length: 26
Connection: keep-alive

{"error":"invalid_client"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Curl 请求中获取状态码和响应体 - Obtain Status Code and Response Body from Curl Request Bash CURL GET Request output to a single JSON file including server response and CURL Information - Bash CURL GET Request output to a single JSON file including server response and CURL Information curl 获取响应状态码并评估 - curl to get response status code and evaluate 使用 curl 上传文件并打印 curl Z78E6221F6393D1356Z81DB3 时获取 http 代码 - Get http code when uploading file using curl and printing curl output 如何从 cURL 输出日志条目中获取原始 URL? - How to get original URL from the cURL output log entry? 如何从带有 Z80791B3AE7002CB88C2468 响应的 curl 命令的 json output 命令获取 json output? - How to get the json output from the curl command with http headers response? 使用curl使用列表文件从网页获取http代码 - Use curl to get http code from webpage, using list file Bash脚本和curl:如何在同一命令中获取状态代码和内容并存储在var中 - Bash script and curl: how to get status code and content and store in var in same command 来自卷曲输出的AWK时间,日期,状态和顺序 - AWK time, date, status and sequence from curl output 如何从shell / shell脚本获取sqlplus命令的输出状态? - How to get output status of sqlplus command from shell/shell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM