Jasper服务器rest_v2 api在python中使用请求

时间:2016-07-04 06:30:17

标签: python django rest python-requests jasperserver

我正在使用jasperserver rest_v2 web服务。我在python中使用请求模块来命中api。在jasper服务器上的报告正在执行和 报告网址如下:

http://localhost:8080/jasperserver/rest_v2/reportExecutions

我得到了像这样的json响应:

{
  "status": "ready",
  "totalPages": 0,
  "requestId": "d0ae905c-1538-4e40-b5ad-c145f521707c",
  "reportURI": "/reports/Invoices/COD",
  "exports": [
    {
      "id": "49f47112-4698-4398-9ed8-a62d552a7aa5",
      "status": "ready",
      "outputResource": {
        "contentType": "application/pdf",
        "fileName": "COD.pdf",
        "outputFinal": true
      }
    }
  ]
}

这意味着我的报告已经执行。

然后我点击以下网址下载报告输出。

http://localhost:8080/jasperserver/rest_v2/reportExecutions/d0ae90c-1538-4e40-b5ad-c145f521707c/exports/49f47112-4698-4398-9ed8-a62d552a7aa5/outputResource

它给出了以下404状态代码的响应:

{
  "message": "Resource d0ae95c-1538-4e40-b5ad-c145f521707c not found.",
  "errorCode": "resource.not.found",
  "parameters": [
    "d0ae95c-1538-4e40-b5ad-c145f521707c"
  ]
}

然后我使用以下网址检查了我的报告状态:

http://localhost:8080/jasperserver/rest_v2/reportExecutions/d0ae905c-1538-4e40-b5ad-c145f521707c

我得到了回复404并跟随json:

{
  "message": "Resource d0ae905c-1538-4e40-b5ad-c145f521707c not found.",
  "errorCode": "resource.not.found",
  "parameters": [
    "d0ae905c-1538-4e40-b5ad-c145f521707c"
  ]
}

使用Postman完全一样正常。 注意:我传递了有效的凭据和相应的标题。

以下是我对此的django观点:

class JasperView(View):
    template = "report.html"

    def get_token(self):
        username = "zeeshan"
        password = "zeeshan"
        token = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
        return token

    def get(self, request, *args, **kwargs):
        return render(request, self.template)

    def post(self, request, *args, **kwargs):
        token = self.get_token()
        data = """
        <reportExecutionRequest>
            <reportUnitUri>/reports/Invoices/COD</reportUnitUri>
            <async>false</async>
            <freshData>false</freshData>
            <saveDataSnapshot>false</saveDataSnapshot>
            <outputFormat>pdf</outputFormat>
            <interactive>true</interactive>
            <ignorePagination>false</ignorePagination>
            <parameters>
                <reportParameter name="StartDate">
                    <value>2016-06-08 00:00:00</value>
                </reportParameter>
                <reportParameter name="EndDate">
                    <value>2016-07-01 00:00:00</value>
                </reportParameter>
                <reportParameter name="ReportID">
                    <value>COD2345</value>
                </reportParameter>
                <reportParameter name="client">
                    <value>Fetchr</value>
                </reportParameter>
                <reportParameter name="manager">
                    <value>Zeeshan Asgar</value>
                </reportParameter>
                <reportParameter name="client_add">
                    <value>Near Bhagat Singh Park, Malviya Nagar, New Delhi-110017</value>
                </reportParameter>
            </parameters>
        </reportExecutionRequest>
        """
        response = requests.post(url="http://localhost:8080/jasperserver/rest_v2/reportExecutions",
                                 headers={
                                     "Authorization": "Basic " + token,
                                     "Accept": "application/json",
                                     "Content-Type": "application/xml",
                                 },
                                 data=data)
        data = response.json()
        request_id = data.get("requestId")
        export_id = data.get("exports")[0].get("id")
        report_url = "http://localhost:8080/jasperserver/rest_v2/reportExecutions/{request_id}/exports/{export_id}/outputResource".format(
            request_id=request_id, export_id=export_id)
        report_resp = requests.get(url=report_url,
                                   headers={"Authorization": "Basic " + token, "Content-Type": "application/xml"})

        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="COD.pdf"'
        response.write(report_resp)
        return response

帮助。

提前致谢。

0 个答案:

没有答案