Power BI Power Query JSON请求负载格式无效问题求助
解决方案
错误原因分析
- 登录请求的用户名、密码等参数被错误放在请求头中,这类身份验证参数应作为JSON请求体传递。
- 手动拼接JSON字符串易出现格式错误(如换行、引号未正确转义),这是触发"Payload in a json request with invalid format"提示的核心原因。
- 使用
Web.Contents时,指定Content参数会自动将请求方法设为POST,无需刻意声明GET(若接口实际要求传递请求体,本质就是POST请求)。
修复后的完整代码
let // 登录请求:参数放入请求体而非请求头 URL1 = "example/login", LoginBody = Json.FromValue([ Token = "TOKEN HERE", Appkey = "APPKEY HERE", Username = "USER HERE", Password = "PASS HERE" ]), Resposta = Web.Contents( URL1, [ Headers = [#"Content-Type" = "application/json"], Content = LoginBody ] ), Fonte1 = Json.Document(Resposta), BearerToken = Fonte1[bearerToken], // 数据请求:用Record定义JSON结构,自动生成规范格式 RequestBody = Json.FromValue([ serviceName = "example.loadRecords", requestBody = [ dataSet = [ rootEntity = "example", includePresentationFields = "S", offsetPage = "0", criteria = [ expression = [ "$" = "(this.example= ? )" ], parameter = [ "$" = "01/08/2023", type = "D" ] ], entity = [ fieldset = [ list = "example,example,example,example,example,example,example" ] ] ] ] ]), URL2 = "example/gateway/v1/example/example.sbr?example=example.example&outputType=json", RequestResponse = Web.Contents( URL2, [ Headers = [ #"Content-Type" = "application/json", Authorization = "Bearer " & BearerToken ], Content = RequestBody ] ), Fonte2 = Json.Document(RequestResponse), Table1 = Record.ToTable(Fonte2) in Table1
关键修复点说明
- 登录请求修正:把
Token/Appkey等参数从请求头移到LoginBody,通过Json.FromValue将Record转换为标准JSON二进制数据,避免手动拼接失误。 - 请求体生成优化:用Power Query的Record结构定义JSON内容,再由
Json.FromValue自动生成符合语法规范的JSON,彻底解决手动字符串拼接导致的格式问题。 - 请求方法适配:
Web.Contents中指定Content后会默认使用POST方法,匹配接口需要传递请求体的实际需求。
内容的提问来源于stack exchange,提问作者webtristinho
相关产品推荐
相关产品推荐

