我在尝试查询我的跨域API时遇到麻烦。curl跨源请求需要用户代理才能工作?
这里是正在发生的事情的一个简单的例子:
curl -i -X POST https://example.com/api/v1/log/create -d 'value=fesfse' -d 'type=3'
HTTP/1.1 403 Forbidden
Date: Thu, 15 Jun 2017 15:07:24 GMT
Content-Type: text/html; charset=iso-8859-1
Content-Length: 219
Set-Cookie: SERVERID31396=234083; path=/; max-age=900
Server: Apache
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Access-Control-Max-Age: 0
Access-Control-Allow-Headers: *
Vary: Accept-Encoding
X-IPLB-Instance: 9386
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /api/v1/log/create
on this server.</p>
</body></html>
但同样的请求,将用户代理,在工作(无论值为)!
curl -i -X POST https://example.com/api/v1/log/create -d 'value=fesfse' -d 'type=3' -H 'User-Agent: toto'
HTTP/1.1 200 OK
Date: Thu, 15 Jun 2017 15:09:27 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Set-Cookie: SERVERID31396=234083; path=/; max-age=900
Server: Apache
X-Powered-By: PHP/7.0.15
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: *
Cache-Control: no-cache, private
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
Access-Control-Max-Age: 0
X-IPLB-Instance: 9387
{"status_code":200,"message":null,"data":null}
它是一个共享的主机上ovh.com服务器,在Apache 2
运行PHP 7.0.15任何人都可以解释这种行为?
Curl有一个内置的用户代理字符串,比如'User-Agent:curl/7.51.0',如果你不指定它的话。发现拒绝服务器请求的服务器并不少见。 –
@AlexHowansky你应该把这个作为答案,因为它看起来正是发生了什么事情 – Marc