api/reports/route endpoint returns an XLSX file

memesaregood2 years ago

Hi, I'm using a /reports/route endpoint, and it produces an Excel file instead of a JSON. I've found two methods that use the same endpoint:
/api/resource/ReportResource.java

@Path("route")
@GET
public Collection<Position> getRoute(
        @QueryParam("deviceId") List<Long> deviceIds,
        @QueryParam("groupId") List<Long> groupIds,
        @QueryParam("from") Date from,
        @QueryParam("to") Date to) throws StorageException {
    permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
    LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds);
    return routeReportProvider.getObjects(getUserId(), deviceIds, groupIds, from, to);
}

    @Path("route")
    @GET
    @Produces(EXCEL)
    public Response getRouteExcel(
            @QueryParam("deviceId") List<Long> deviceIds,
            @QueryParam("groupId") List<Long> groupIds,
            @QueryParam("from") Date from,
            @QueryParam("to") Date to,
            @QueryParam("mail") boolean mail) throws StorageException {
        permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
        return executeReport(getUserId(), mail, stream -> {
            LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds);
            routeReportProvider.getExcel(stream, getUserId(), deviceIds, groupIds, from, to);
        });
    }

How do I tell the program I need the JSON response? I've already set Content-Type header to application/json.

Anton Tananaev2 years ago

You have to pass the Accept header.

memesaregood2 years ago

Yep, that was it. Thanks.

MDAa year ago
const response = await fetch(`/api/reports/route?${query.toString()}`, {
          headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
        });

but it doesnot work

MDAa year ago

And this my java code:

   @Path("route")
    @GET
    public Collection<Position> getRoute(
            @QueryParam("deviceId") List<Long> deviceIds,
            @QueryParam("groupId") List<Long> groupIds,
            @QueryParam("from") Date from,
            @QueryParam("to") Date to) throws StorageException {
        permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
        LogAction.logReport(getUserId(), "route", from, to, deviceIds, groupIds);
        return routeReportProvider.getObjects(getUserId(), deviceIds, groupIds, from, to);
    }
MDAa year ago

It works on local:3000 but when I use ReverseProxyInboundRule , It does not work on my main domain.

MDAa year ago

and this is my web config

    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="ReverseProxyInboundRule3" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="ws://gps.xxxx.ir*" ignoreCase="true" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="ws://localhost:8082{R:1}" appendQueryString="true" logRewrittenUrl="true" />
                    <serverVariables>
                        <set name="HTTP_SEC_WEBSOCKET_EXTENSIONS" value="" />
                    </serverVariables>
                </rule>
                <rule name="ReverseProxyInboundRule2" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="wss://gps.xxxx.ir*" ignoreCase="true" />
                    <action type="Rewrite" url="ws://localhost:8082{R:1}" appendQueryString="true" logRewrittenUrl="true" />
                    <serverVariables>
                        <set name="HTTP_SEC_WEBSOCKET_EXTENSIONS" value="" />
                    </serverVariables>
                </rule>
                  <rule name="ReverseProxyInboundRule4" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="ws://gps2.xxxx.ir*" ignoreCase="true" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="ws://localhost:8082{R:1}" appendQueryString="true" logRewrittenUrl="true" />
                    <serverVariables>
                        <set name="HTTP_SEC_WEBSOCKET_EXTENSIONS" value="" />
                    </serverVariables>
                </rule>
                <rule name="ReverseProxyInboundRule5" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="wss://gps2.xxxx.ir*" ignoreCase="true" />
                    <action type="Rewrite" url="ws://localhost:8082{R:1}" appendQueryString="true" logRewrittenUrl="true" />
                    <serverVariables>
                        <set name="HTTP_SEC_WEBSOCKET_EXTENSIONS" value="" />
                    </serverVariables>
                </rule>      
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:8082/{R:1}" />
                    <serverVariables>
                        <set name="HTTP_SEC_WEBSOCKET_EXTENSIONS" value="" />
                    </serverVariables>
                </rule>
            </rules>
            <outboundRules>
                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:8082/(.*)" />
                    <action type="Rewrite" value="http{R:1}://gps.xxxx.ir/{R:2}" />
                </rule>				
                <rule name="ReverseProxyOutboundRule2" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:8082/(.*)" />
                    <action type="Rewrite" value="http{R:1}://gps2.xxxx.ir/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
        <urlCompression doStaticCompression="false" doDynamicCompression="false" />
    </system.webServer>
MDAa year ago
const response = await fetch(`/api/reports/route?${query.toString()}`, {
          headers: { Accept: 'application/json', 'Content-Type': 'application/json; charset=utf-8' },
        });

Finally it works. add 'Content-Type': 'application/json; charset=utf-8' to your angular project