Wrong MIME type ...
Thursday, 11 November 2021 · 5 messages
JR
Jonathan Robie11 Nov 2021 02:32 #
Using RESTXQ in an application for the first time. I expected this function to have the MIME type application/json:
declare
%rest:path("/api/abbott-smith/index")
%rest:GET
%rest:produces("application/json")
function as:index()
{ array { $as:abbott-smith/*:entry ! @lemma ! string() }
! serialize(., map {'method' : 'json'})
};
But both POSTMAN and WADL give me application/xml as the mime type:
<wadl:resource path="/api/abbott-smith/index">
<wadl:method name="GET">
<wadl:request/>
<wadl:response>
<wadl:representation mediaType="application/xml"/>
</wadl:response>
</wadl:method>
</wadl:resource>What am I doing wrong?
Jonathan
KK
Kristian Kankainen11 Nov 2021 05:29 #
Hi Jonathan,
Have you tried setting the media-type or method parameters to application/json instead of only the produces parameter, as explained here [1].
[1] https://docs.basex.org/wiki/REST#Content_Type <https://docs.basex.org/wiki/REST#Content_Type>
Best regards,
Kristian Kankainen
Quoted text · Jonathan Robie · 28 lines
> On 11. Nov 2021, at 02:32, Jonathan Robie <jonathan.robie at gmail.com> wrote: > > Using RESTXQ in an application for the first time. I expected this function to have the MIME type application/json: > > declare > %rest:path("/api/abbott-smith/index") > %rest:GET > %rest:produces("application/json") > function as:index() > { > array { $as:abbott-smith/*:entry ! @lemma ! string() } > ! serialize(., map {'method' : 'json'}) > }; > > But both POSTMAN and WADL give me application/xml as the mime type: > > <wadl:resource path="/api/abbott-smith/index"> > <wadl:method name="GET"> > <wadl:request/> > <wadl:response> > <wadl:representation mediaType="application/xml"/> > </wadl:response> > </wadl:method> > </wadl:resource> > > What am I doing wrong? > > Jonathan
JR
Jonathan Robie11 Nov 2021 15:11 #
I want the query on the server side to specify the Content-Type header for the response. I didn't see how to do that from the link you provided.
This seems to work - is there a better way?
declare function as:json-response-headers()
{
<rest:response>
<http:response status="404">
<http:header name="Content-Language" value="el"/>
<http:header name="Content-Type" value="application/json;
charset=utf-8"/>
</http:response>
</rest:response>};
declare
%rest:path("/api/abbott-smith/index")
%rest:GET
%rest:produces("application/json")
function as:index()
{
as:json-response-headers(), array { $as:abbott-smith/*:entry ! @lemma ! string() }
! serialize(., map {'method' : 'json'})
};
Jonathan
[1] https://docs.basex.org/wiki/RESTXQ#Custom_Response
Quoted text · Kristian Kankainen · 50 lines
On Wed, Nov 10, 2021 at 11:29 PM Kristian Kankainen <kristian at keeleleek.ee> wrote: > Hi Jonathan, > > Have you tried setting the media-type or method parameters to > application/json instead of only the produces parameter, as explained here > [1]. > > > [1] https://docs.basex.org/wiki/REST#Content_Type > > Best regards, > Kristian Kankainen > > > > On 11. Nov 2021, at 02:32, Jonathan Robie <jonathan.robie at gmail.com> > wrote: > > Using RESTXQ in an application for the first time. I expected this > function to have the MIME type application/json: > > declare > %rest:path("/api/abbott-smith/index") > %rest:GET > %rest:produces("application/json") > function as:index() > { > array { $as:abbott-smith/*:entry ! @lemma ! string() } > ! serialize(., map {'method' : 'json'}) > }; > > But both POSTMAN and WADL give me application/xml as the mime type: > > <wadl:resource path="/api/abbott-smith/index"> > <wadl:method name="GET"> > <wadl:request/> > <wadl:response> > <wadl:representation mediaType="application/xml"/> > </wadl:response> > </wadl:method> > </wadl:resource> > > > What am I doing wrong? > > Jonathan > > >
CG
Christian Grün11 Nov 2021 19:19 #
Hi Jonathan,
As Kristian indicated, it suffices to specify the media-type or method parameter. The content type will be generated by BaseX (see the attached example).
If you want to return a non-standard or custom Content-Type header, you’ll indeed need to construct your own response, either manually or via the
web:response-header helper function.
Hope this helps,
Christian
declare
%rest:path('json')
%output:method('json')
function page:test() {
map { 1: 2 }
};Quoted text · Jonathan Robie · 1 line
> curl -i "http://localhost:8984/json"
HTTP/1.1 200 OK
Date: Thu, 11 Nov 2021 18:14:42 GMT
Content-Type: application/json;charset=utf-8
Server-Timing: parse;dur=0.05 ms,compile;dur=0.12 ms,evaluate;dur=0.0 ms,serialize;dur=0.0 ms
Content-Length: 14
Server: Jetty(9.4.44.v20210927)
{
"1": 2
}Quoted text · Jonathan Robie · 97 lines
On Thu, Nov 11, 2021 at 2:11 PM Jonathan Robie <jonathan.robie at gmail.com> wrote: > > I want the query on the server side to specify the Content-Type header for the response. I didn't see how to do that from the link you provided. > > This seems to work - is there a better way? > > > declare function as:json-response-headers() > > { > > <rest:response> > > <http:response status="404"> > > <http:header name="Content-Language" value="el"/> > > <http:header name="Content-Type" value="application/json; charset=utf-8"/> > > </http:response> > > </rest:response> > > }; > > > declare > > %rest:path("/api/abbott-smith/index") > > %rest:GET > > %rest:produces("application/json") > > function as:index() > > { > > as:json-response-headers(), > > > array { $as:abbott-smith/*:entry ! @lemma ! string() } > > ! serialize(., map {'method' : 'json'}) > > }; > > > Jonathan > > [1] https://docs.basex.org/wiki/RESTXQ#Custom_Response > > On Wed, Nov 10, 2021 at 11:29 PM Kristian Kankainen <kristian at keeleleek.ee> wrote: >> >> Hi Jonathan, >> >> Have you tried setting the media-type or method parameters to application/json instead of only the produces parameter, as explained here [1]. >> >> >> [1] https://docs.basex.org/wiki/REST#Content_Type >> >> Best regards, >> Kristian Kankainen >> >> >> >> On 11. Nov 2021, at 02:32, Jonathan Robie <jonathan.robie at gmail.com> wrote: >> >> Using RESTXQ in an application for the first time. I expected this function to have the MIME type application/json: >> >> declare >> %rest:path("/api/abbott-smith/index") >> %rest:GET >> %rest:produces("application/json") >> function as:index() >> { >> array { $as:abbott-smith/*:entry ! @lemma ! string() } >> ! serialize(., map {'method' : 'json'}) >> }; >> >> But both POSTMAN and WADL give me application/xml as the mime type: >> >> <wadl:resource path="/api/abbott-smith/index"> >> <wadl:method name="GET"> >> <wadl:request/> >> <wadl:response> >> <wadl:representation mediaType="application/xml"/> >> </wadl:response> >> </wadl:method> >> </wadl:resource> >> >> >> What am I doing wrong? >> >> Jonathan >> >>
JR
Jonathan Robie11 Nov 2021 22:13 #
Works like a charm! Thanks, Kristian and Christian!
Jonathan
Quoted text · Christian Grün · 143 lines
On Thu, Nov 11, 2021 at 1:19 PM Christian Grün <christian.gruen at gmail.com> wrote: > Hi Jonathan, > > As Kristian indicated, it suffices to specify the media-type or method > parameter. The content type will be generated by BaseX (see the > attached example). > > If you want to return a non-standard or custom Content-Type header, > you’ll indeed need to construct your own response, either manually or > via the web:response-header helper function. > > Hope this helps, > Christian > > declare > %rest:path('json') > %output:method('json') > function page:test() { > map { 1: 2 } > }; > > > curl -i "http://localhost:8984/json" > HTTP/1.1 200 OK > Date: Thu, 11 Nov 2021 18:14:42 GMT > Content-Type: application/json;charset=utf-8 > Server-Timing: parse;dur=0.05 ms,compile;dur=0.12 ms,evaluate;dur=0.0 > ms,serialize;dur=0.0 ms > Content-Length: 14 > Server: Jetty(9.4.44.v20210927) > > { > "1": 2 > } > > > > On Thu, Nov 11, 2021 at 2:11 PM Jonathan Robie <jonathan.robie at gmail.com> > wrote: > > > > I want the query on the server side to specify the Content-Type header > for the response. I didn't see how to do that from the link you provided. > > > > This seems to work - is there a better way? > > > > > > declare function as:json-response-headers() > > > > { > > > > <rest:response> > > > > <http:response status="404"> > > > > <http:header name="Content-Language" value="el"/> > > > > <http:header name="Content-Type" value="application/json; > charset=utf-8"/> > > > > </http:response> > > > > </rest:response> > > > > }; > > > > > > declare > > > > %rest:path("/api/abbott-smith/index") > > > > %rest:GET > > > > %rest:produces("application/json") > > > > function as:index() > > > > { > > > > as:json-response-headers(), > > > > > > array { $as:abbott-smith/*:entry ! @lemma ! string() } > > > > ! serialize(., map {'method' : 'json'}) > > > > }; > > > > > > Jonathan > > > > [1] https://docs.basex.org/wiki/RESTXQ#Custom_Response > > > > On Wed, Nov 10, 2021 at 11:29 PM Kristian Kankainen < > kristian at keeleleek.ee> wrote: > >> > >> Hi Jonathan, > >> > >> Have you tried setting the media-type or method parameters to > application/json instead of only the produces parameter, as explained here > [1]. > >> > >> > >> [1] https://docs.basex.org/wiki/REST#Content_Type > >> > >> Best regards, > >> Kristian Kankainen > >> > >> > >> > >> On 11. Nov 2021, at 02:32, Jonathan Robie <jonathan.robie at gmail.com> > wrote: > >> > >> Using RESTXQ in an application for the first time. I expected this > function to have the MIME type application/json: > >> > >> declare > >> %rest:path("/api/abbott-smith/index") > >> %rest:GET > >> %rest:produces("application/json") > >> function as:index() > >> { > >> array { $as:abbott-smith/*:entry ! @lemma ! string() } > >> ! serialize(., map {'method' : 'json'}) > >> }; > >> > >> But both POSTMAN and WADL give me application/xml as the mime type: > >> > >> <wadl:resource path="/api/abbott-smith/index"> > >> <wadl:method name="GET"> > >> <wadl:request/> > >> <wadl:response> > >> <wadl:representation mediaType="application/xml"/> > >> </wadl:response> > >> </wadl:method> > >> </wadl:resource> > >> > >> > >> What am I doing wrong? > >> > >> Jonathan > >> > >> >
Written for BaseX 9.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery