Issues posting/putting JSON content with markup in it via http module.

Monday, 16 February 2015 · 3 messages · last reply 24 Mar 2015
JW
James Wright16 Feb 2015 22:51 #
Hello everyone, First, I want to say that I really like the JSON improvements in BaseX 8.0. Great job. I am however having an issue when posting JSON with markup in it. For example: let $out := map { name: 'myView', markup: "<div name='value'>Some other stuff</div>" }
http:send-request(<http:request method="POST" />, uri, $out)
The issue is that the resulting JSON body being posted has the 'markup' property xml escaped. IE:
{ "name": "myView",
  "markup": "&lt;div name='value'&gt;Some other ..... "
}
I had a similar issue with file:write but resolved that by using file:write-string. I have tried calling json:serialize on $out manually as well as declaring the option output:method to 'json'. I even tried changing the request object by inserting a http:body. IE:
<http:request method="GET">
   <http:body content-type="application/json">
      {json:serialize($out)}
   </http>
</http>
Lastly, I have tried declaring the cdata-section option: declare option output:cdata-section-elements 'http:body'; Still no go... Is this not possible? One option would be to serialize the content using file:write and then call curl. This is a workaround I have used in the past however I was hoping I was behind that now with the latest improvements to XQuery 3.1. Maybe I need to send the http:request object as a map instead of an xml node? Thanks again for your time on all of this. - James
CG
Christian Grün17 Feb 2015 21:37 #
Hi James,
Quoted text · James Wright · 2 lines
> I am however having an issue when posting JSON with markup in it. For > example:
I tried to reproduce your example, but it didn't work out. It seems you were storing data in a database, right? Will the data be stored in a wrong format, or is the subsequent output (serialization) that gives wrongly escaped results? Could you possibly give me a little complete example that we can try out? Thanks in advance, Christian
Quoted text · James Wright · 41 lines
> > let $out := map { > name: 'myView', > markup: "<div name='value'>Some other stuff</div>" > } > > http:send-request(<http:request method="POST" />, uri, $out) > > The issue is that the resulting JSON body being posted has the 'markup' > property xml escaped. IE: > > { "name": "myView", > "markup": "&lt;div name='value'&gt;Some other ..... " > } > > I had a similar issue with file:write but resolved that by using > file:write-string. > > I have tried calling json:serialize on $out manually as well as declaring > the option output:method to 'json'. I even tried changing the request object > by inserting a http:body. IE: > > <http:request method="GET"> > <http:body content-type="application/json"> > {json:serialize($out)} > </http> > </http> > > Lastly, I have tried declaring the cdata-section option: > declare option output:cdata-section-elements 'http:body'; > > Still no go... > > Is this not possible? One option would be to serialize the content using > file:write and then call curl. This is a workaround I have used in the past > however I was hoping I was behind that now with the latest improvements to > XQuery 3.1. Maybe I need to send the http:request object as a map instead of > an xml node? > > Thanks again for your time on all of this. > - James
JW
James Wright24 Mar 2015 02:28 #
Hello Christian, Thanks for the response. I figured out how to get around my issue. Add the method="text" attribute to the http:body.
<http:body media-type="application/json"
method="text">{json:serialize($body)}</http:body>
- James
Quoted text · Christian Grün · 63 lines
On Tue, Feb 17, 2015 at 1:37 PM, Christian Grün <christian.gruen at gmail.com> wrote: > Hi James, > > > I am however having an issue when posting JSON with markup in it. For > > example: > > I tried to reproduce your example, but it didn't work out. It seems > you were storing data in a database, right? Will the data be stored in > a wrong format, or is the subsequent output (serialization) that gives > wrongly escaped results? Could you possibly give me a little complete > example that we can try out? > > Thanks in advance, > Christian > > > > > > let $out := map { > > name: 'myView', > > markup: "<div name='value'>Some other stuff</div>" > > } > > > > http:send-request(<http:request method="POST" />, uri, $out) > > > > The issue is that the resulting JSON body being posted has the 'markup' > > property xml escaped. IE: > > > > { "name": "myView", > > "markup": "&lt;div name='value'&gt;Some other ..... " > > } > > > > I had a similar issue with file:write but resolved that by using > > file:write-string. > > > > I have tried calling json:serialize on $out manually as well as declaring > > the option output:method to 'json'. I even tried changing the request > object > > by inserting a http:body. IE: > > > > <http:request method="GET"> > > <http:body content-type="application/json"> > > {json:serialize($out)} > > </http> > > </http> > > > > Lastly, I have tried declaring the cdata-section option: > > declare option output:cdata-section-elements 'http:body'; > > > > Still no go... > > > > Is this not possible? One option would be to serialize the content using > > file:write and then call curl. This is a workaround I have used in the > past > > however I was hoping I was behind that now with the latest improvements > to > > XQuery 3.1. Maybe I need to send the http:request object as a map > instead of > > an xml node? > > > > Thanks again for your time on all of this. > > - James >

⚡Generated with XQuery