OpenSearch 1.3:API创建索引模式时请求体格式报错求助
解决OpenSearch 1.3 API创建索引模式的请求体错误
错误原因分析
你遇到的400错误是因为请求体格式不符合OpenSearch Dashboards Saved Objects API的要求。该API期望请求体包含attributes对象,而非你当前使用的index_pattern嵌套结构。
修正后的PowerShell脚本
$baseuri = "https://<clusterendpoint>.<region>.es.amazonaws.com" # 获取认证Cookie $authbody = @" {"username":"$apiUserName", "password":"$apiPassword"} "@ $authUri = "$baseuri/_dashboards/auth/login" Invoke-RestMethod -Method Post -Uri $authUri -Headers @{"osd-xsrf"="true"} -ContentType "application/json" -Body $authbody -SessionVariable S1 # 创建索引模式 $indexid = "indexname" $body=@" { "attributes": { "title": "indexname-*", "timeFieldName": "@timestamp" } } "@ $uri = "$baseuri/_dashboards/api/saved_objects/index-pattern/$indexid" Invoke-RestMethod -Method Post -Uri $uri -Headers @{"osd-xsrf"="true"} -ContentType "application/json" -WebSession $S1 -Body $body -Verbose
关键说明
- 请求体格式修正:将原
index_pattern字段替换为attributes,直接在该对象下定义索引模式的title和timeFieldName。 - API路径区分:不要使用Elastic的
/api/index_patterns路径,OpenSearch中创建索引模式需使用/_dashboards/api/saved_objects/index-pattern,否则会误创建物理索引而非索引模式。 - 避免转换错误:直接使用JSON字符串作为请求体,无需通过
ConvertFrom-Json转换,防止PowerShell转换过程中丢失结构导致null错误。
内容的提问来源于stack exchange,提问作者Clae
相关产品推荐
相关产品推荐

