如何使用 cURL 调试 CORS 请求? [英] How can you debug a CORS request with cURL?
问题描述
如何使用 cURL 调试 CORS 请求?到目前为止,我找不到任何方法来模拟"预检请求.
How can you debug CORS requests using cURL? So far I couldn't find any way to "simulate" the preflight request .
推荐答案
以下是使用 curl 调试 CORS 请求的方法.
Here's how you can debug CORS requests using curl.
使用 cUrl 发送常规 CORS 请求:
curl -H "Origin: http://example.com" --verbose
https://www.googleapis.com/discovery/v1/apis?fields=
-H "Origin: http://example.com"
标志是发出请求的第三方域.替换为任何您的域.
The -H "Origin: http://example.com"
flag is the third party domain making the request. Substitute in whatever your domain is.
--verbose
标志打印出整个响应,以便您可以看到请求和响应标头.
The --verbose
flag prints out the entire response so you can see the request and response headers.
我在上面使用的 url 是对支持 CORS 的 Google API 的示例请求,但您可以替换为您正在测试的任何 url.
The url I'm using above is a sample request to a Google API that supports CORS, but you can substitute in whatever url you are testing.
响应应包含 Access-Control-Allow-Origin
标头.
使用 cUrl 发送预检请求:
curl -H "Origin: http://example.com"
-H "Access-Control-Request-Method: POST"
-H "Access-Control-Request-Headers: X-Requested-With"
-X OPTIONS --verbose
https://www.googleapis.com/discovery/v1/apis?fields=
这看起来类似于常规的 CORS 请求,但有一些补充:
This looks similar to the regular CORS request with a few additions:
-H
标志向服务器发送额外的预检请求标头
The -H
flags send additional preflight request headers to the server
-X OPTIONS
标志表明这是一个 HTTP OPTIONS 请求.
The -X OPTIONS
flag indicates that this is an HTTP OPTIONS request.
如果预检请求成功,响应应包括Access-Control-Allow-Origin
、Access-Control-Allow-Methods
和Access-Control-Allow-Headers
响应头.如果预检请求不成功,则不应出现这些标头,否则 HTTP 响应不会是 200.
If the preflight request is successful, the response should include the Access-Control-Allow-Origin
, Access-Control-Allow-Methods
, and Access-Control-Allow-Headers
response headers. If the preflight request was not successful, these headers shouldn't appear, or the HTTP response won't be 200.
您还可以使用 -H
标志指定其他标头,例如 User-Agent
.
You can also specify additional headers, such as User-Agent
, by using the -H
flag.
这篇关于如何使用 cURL 调试 CORS 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!