gpt4 book ai didi

json - 在JSONException之后读取请求主体

转载 作者:行者123 更新时间:2023-12-02 14:53:58 26 4
gpt4 key购买 nike

我有一个Grails Controller ,它接受带有JSON正文的POST请求。在某些请求中,JSON格式不正确,我得到了JSONException。如何获取请求正文以进行记录或检查(我并不总是可以访问客户端)? Grails request.JSON已经使用了流,因此看来我无法通过request.body访问它。

这是我的 Controller Action :

def setScore(ScoreCommand scoreCommand) {
try {
def context = request.JSON.optJSONObject("context")
userService.updateContext(context)
} catch (ConverterException ce) {
log.error("Error parsing JSON")
// I want to log the malformed JSON body
}
def scoreResponse
if (!scoreCmd.validate()) {
scoreResponse = errorResponse(scoreCommand)
} else {
def scoreResult = gameService.setScore(scoreCommand)
scoreResponse = buildResponse(scoreResult)
}
render scoreResponse as JSON
}

最佳答案

这样行吗?

def json = request.JSON

try {
def context = json.optJSONObject("context")
userService.updateContext(context)
}catch(e) {
log.error json.toString()
}

关于json - 在JSONException之后读取请求主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14447783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com