Spring Boot Actuator 2.0.0 ShutDown端点通过POST请求调用时返回错误415

时间:2018-07-25 05:51:50

标签: powershell spring-boot spring-boot-actuator

我使用spring-boot-starter-actuator:2.0.0.RELEASE并启用了以下功能:

management.endpoint.shutdown.enabled=true
management.endpoints.web.exposure.include=shutdown
management.security.basic.enabled=false

从PowerShell命令中,我使用以下命令调用了shutdown端点:

$endpointUrl = "http://localhost:8200/actuator/shutdown"
Invoke-WebRequest -Uri $endPointUrl -Method POST

我得到:

Invoke-WebRequest : The remote server returned an error: (415).
At line:1 char:1
+ Invoke-WebRequest -Uri $endPointUrl -Method POST
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

在我的WebSecurityConfigurerAdapter中,我有:

http
    .authorizeRequests()
    .csrf()
    .disable();

我的Web请求调用出了什么问题?

1 个答案:

答案 0 :(得分:1)

Invoke-WebRequest中的默认ContentType是application/x-www-form-urlencoded

请参阅:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6

  

-ContentType   指定Web请求的内容类型。如果忽略此参数,并且请求方法为POST,则Invoke-WebRequest会将内容类型设置为application / x-www-form-urlencoded。否则,将在呼叫中未指定内容类型。

我在Postman中尝试过,shutdown端点可以与application/json一起使用,也可以完全没有ContentType头(在Spring文档中找不到)。但是,将此标头设置为application/jsonhttps://github.com/PowerShell/PowerShell/issues/3131)可能会出现一些问题 因此可能只是尝试设置-ContentType 'application/json' -Body "null"。我没有Windows计算机可以尝试。