Bootstrap

cannot unmarshal string into Go value of type models.SaveDashboardCommand

cannot unmarshal string into Go value of type models.SaveDashboardCommand

在Java服务端使用retrofit发起http请求,创建grafana告警面板时,报上述错误

打印retrofit 请求日志如下

–> POST http://xxxxx/api/dashboards/db http/1.1
Content-Type: application/json
Content-Length: 62
Authorization:
DgxOH0=
Host: 9.218.23.159:8081
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.14.9
“{“folderId”:xxx,“overwrite”:false,“dashboard”:”"}"
–> END POST (62-byte body)
<-- 400 Bad Request http://xxxx/api/dashboards/db (96ms)

原因: 请求体json被转义、请求体的类型类型是String,

@POST("dashboards/db")
@Headers("Content-Type: application/json")
Response<Object> method(@Body String param);

解决办法:

需修改为RequestBody

@POST("dashboards/db")
@Headers("Content-Type: application/json")
Response<Object> method(@Body RequestBody param);

使用时

RequestBody body = RequestBody.create(
                MediaType.parse("application/json"), stringparam);
;