From 2cd861637a35eb27aee72100d201012480d07078 Mon Sep 17 00:00:00 2001 From: cjy513203427 Date: Fri, 13 Dec 2024 09:50:54 +0100 Subject: [PATCH] fix Json parse error for POST and PUT --- src/Sample/PersonREST.cls | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Sample/PersonREST.cls b/src/Sample/PersonREST.cls index 2ed114d..7d53ecb 100644 --- a/src/Sample/PersonREST.cls +++ b/src/Sample/PersonREST.cls @@ -82,7 +82,7 @@ ClassMethod GetPerson(id As %Integer) As %Status Set person = ##class(Sample.Person).%OpenId(id) - If '$IsObject(person) Quit ..Http404() + If '$IsObject(person) Quit ..ReportHttpStatusCode() Do person.%JSONExport() @@ -92,21 +92,22 @@ ClassMethod GetPerson(id As %Integer) As %Status /// Creates a new Sample.Person record ClassMethod CreatePerson() As %Status { - #dim tSC As %Status = $$$OK + #dim tSC As %Status = $$$OK #dim e As %Exception.AbstractException + Set person = ##class(Sample.Person).%New() - Set data = {}.%FromJSON(%request.Content) + Set requestContent = %request.Content + Set requestContentStr = requestContent.%ToJSON() + Set data = {}.%FromJSON(requestContentStr) - - $$$TOE(tSC,person.%JSONImport(data)) - $$$TOE(tSC,person.%Save()) + $$$TOE(tSC, person.%JSONImport(data)) + $$$TOE(tSC, person.%Save()) Set %response.Status = 204 Set %response.ContentType = ..#CONTENTTYPEJSON - //d data.%ToJSON() Do person.%JSONExport() - Quit tSC + QUIT tSC } /// Update a record in Sample.Person with id @@ -115,9 +116,11 @@ ClassMethod UpdatePerson(id As %Integer) As %Status #dim tSC As %Status = $$$OK #dim e As %Exception.AbstractException Set person = ##class(Sample.Person).%OpenId(id) - If '$IsObject(person) Return ..Http404() - Set data = {}.%FromJSON(%request.Content) + If '$IsObject(person) Return ..ReportHttpStatusCode() + Set requestContent = %request.Content + Set requestContentStr = requestContent.%ToJSON() + Set data = {}.%FromJSON(requestContentStr) $$$TOE(tSC,person.%JSONImport(data)) $$$TOE(tSC,person.%Save()) @@ -141,7 +144,6 @@ ClassMethod DeletePerson(id As %Integer) As %Status $$$TOE(tSC,person.%DeleteId(id)) Set %response.Status = 200 - Set %response.ContentType = ..#CONTENTTYPEJSON Quit tSC