user7544125
user7544125

Reputation:

passing json to cfhttpparam

I am using ColdFusion 2016 and here is what i am doing:

<cfhttp method="put" url="https://www.colorfulapi.com/testpage/#arguments.Name#" username="#request.APIusername#" password="#request.APIToken#" result="results">
    <cfhttpparam type="header" name="Content-Type" value="application/json">
    <cfif isStruct(arguments.structform) AND !StructIsEmpty(arguments.structform)>
    <CFHTTPPARAM VALUE="'#serializeJSON(stFields)#'" TYPE="body">
    </cfif>
  </cfhttp>

st are like this: if i dump them

'{"ONE":{"GROUP":"my group"}}'

if it goes to the cfhttpparam using serializeJSON it shows me as:

"error":"JSON error: 822: unexpected token at ''{\"ONE\":{\"GROUP\":\"my  group\"}}''"}

i tried the same code in POSTman, but in POSTman for JSOn sending i had to use the body as raw and choose content as application/json and it worked there

This is how i am generating my stFields

<cfset stFields = StructNew()>
  <cfset stFields.one = arguments.structform>
  <cfdump var="'#serializeJSON(stFields)#'"> 

Upvotes: 0

Views: 1362

Answers (1)

Alex
Alex

Reputation: 7833

Remove the single quotes:

<CFHTTPPARAM VALUE="#serializeJSON(stFields)#" TYPE="body">

Old answer:

The serializeJSON function serializes objects (structs, arrays, queries, components, simple values) and outputs the content as a JSON string. Your stFields is already a JSON string, thus there's no need to serialize it (again):

<CFHTTPPARAM VALUE="#stFields#" TYPE="body">

Upvotes: 1

Related Questions