Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cURL 响应状态码的相关例子 #243

Open
islishude opened this issue Nov 17, 2020 · 0 comments
Open

cURL 响应状态码的相关例子 #243

islishude opened this issue Nov 17, 2020 · 0 comments
Assignees
Labels

Comments

@islishude
Copy link
Owner

islishude commented Nov 17, 2020

  1. 怎么获取返回 HTTP 状态码

curl -I 可以返回响应头,当然也就包含了响应状态码。

如果想只要状态码,那么可以这样做:

$ curl -sS -w "%{http_code}" -o /dev/null https://www.baidu.com
200

如果请求失败,那么返回 000

  1. HTTP 响应状态码 500/400 那么 curl 运行是否也是失败?

$ cat main.go && go run main.go&
package main

import (
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(500)
	})
	log.Println("listen and serving...")
	_ = http.ListenAndServe(":8080", nil)
}
$ curl locahost:8080 
$ echo $?
0

如果想要 shell 返回错误码,可以加上 -f 参数,非 200 会返回 shell 22 error code

@islishude islishude self-assigned this Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant