Hi William,

The response type of an SWP service is fixed, so a JSON service still should 
return JSON in case of error.

The HTTP response status code can be set using ui:setResponseHeader:

    <ui:setResponseHeader ui:statusCode="{= 404 }"/>

Personally I would say that a simple JSON object of the form {"error":"No 
results found"} with an appropriate status code is a perfectly good response 
and should be easy enough to handle by the service’s clients.

FWIW, here is an idiomatic way of checking for an empty SPARQL result set:

    <ui:group let:rs="{# … }">
        <ui:if ui:condition="{= spr:isEmpty(?rs) }">
            … handle empty result set …
        </ui:if>
        <ui:else>
            ...
            <ui:forEach ui:resultSet="{= ?rs }">
                ...
            </ui:forEach>
            ...
        </ui:else>
     </ui:group>

Hope that helps,
Richard


> On 12 Nov 2018, at 19:30, William Ramos <[email protected]> wrote:
> 
> Hi, community;
> 
> I have a SWON service which contains a SPARQL query, in case the query is not 
> empty the service responds with a JSON object. But in case the query is empty 
> I would like to return a non JSON response, ideally an HTTP 404 with a 
> payload message like "No results found."
> 
> Is it possible to change the response type dynamically to achieve this?
> 
> For reference, the service ui:prototype is like this:
> <ui:group>
>     <ui:setGlobalVar ui:name="empty" ui:value="{= xsd:boolean(true) }"/>
>     <ui:forEach ui:resultSet="{#
>                               ...
>                               }" ui:separator=",">
>         <ui:setGlobalVar ui:name="empty" ui:value="{= xsd:boolean(false) }"/>
>         <swon:Object>
>             ...
>         </swon:Object>
>     </ui:forEach>
>     <ui:if ui:condition="{= ui:globalVarValue(&quot;empty&quot;) = true }">
>         <swon:Object>
>             <swon:Value arg:name="Error" arg:value="No results found."/>
>         </swon:Object>
>     </ui:if>
> </ui:group>
> 
> Thanks in advance.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "TopBraid Suite Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to