title: Jasper server rest_v2 api using requests in python
body: <p>I m using jasperserver rest_v2 web service. I am using requests module in python to hit the api. The report which is on jasper server is executing and
  report url is following:</p>
  
  <p><a href="http://localhost:8080/jasperserver/rest_v2/reportExecutions" rel="nofollow">http://localhost:8080/jasperserver/rest_v2/reportExecutions</a></p>
  
  <p>and I m gettng a json response like that:</p>
  
  <pre><code>{
    "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
        }
      }
    ]
  }
  </code></pre>
  
  <p>It means my report was executed. </p>
  
  <p>Then I hit the following url to download the report output.</p>
  
  <p><a href="http://localhost:8080/jasperserver/rest_v2/reportExecutions/d0ae90c-1538-4e40-b5ad-c145f521707c/exports/49f47112-4698-4398-9ed8-a62d552a7aa5/outputResource" rel="nofollow">http://localhost:8080/jasperserver/rest_v2/reportExecutions/d0ae90c-1538-4e40-b5ad-c145f521707c/exports/49f47112-4698-4398-9ed8-a62d552a7aa5/outputResource</a></p>
  
  <p>It gives me the following response with 404 status code:</p>
  
  <pre><code>{
    "message": "Resource d0ae95c-1538-4e40-b5ad-c145f521707c not found.",
    "errorCode": "resource.not.found",
    "parameters": [
      "d0ae95c-1538-4e40-b5ad-c145f521707c"
    ]
  }
  </code></pre>
  
  <p>then I checked my report status using the following url:</p>
  
  <p><a href="http://localhost:8080/jasperserver/rest_v2/reportExecutions/d0ae905c-1538-4e40-b5ad-c145f521707c" rel="nofollow">http://localhost:8080/jasperserver/rest_v2/reportExecutions/d0ae905c-1538-4e40-b5ad-c145f521707c</a></p>
  
  <p>I m getting a response 404 and following json:</p>
  
  <pre><code>{
    "message": "Resource d0ae905c-1538-4e40-b5ad-c145f521707c not found.",
    "errorCode": "resource.not.found",
    "parameters": [
      "d0ae905c-1538-4e40-b5ad-c145f521707c"
    ]
  }
  </code></pre>
  
  <p>And the exact same thing is working fine using Postman.
  NOTE: I m passing the valid credentials and appropriate headers.</p>
  
  <p>Below is my django view for this:</p>
  
  <pre><code>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 = """
          &lt;reportExecutionRequest&gt;
              &lt;reportUnitUri&gt;/reports/Invoices/COD&lt;/reportUnitUri&gt;
              &lt;async&gt;false&lt;/async&gt;
              &lt;freshData&gt;false&lt;/freshData&gt;
              &lt;saveDataSnapshot&gt;false&lt;/saveDataSnapshot&gt;
              &lt;outputFormat&gt;pdf&lt;/outputFormat&gt;
              &lt;interactive&gt;true&lt;/interactive&gt;
              &lt;ignorePagination&gt;false&lt;/ignorePagination&gt;
              &lt;parameters&gt;
                  &lt;reportParameter name="StartDate"&gt;
                      &lt;value&gt;2016-06-08 00:00:00&lt;/value&gt;
                  &lt;/reportParameter&gt;
                  &lt;reportParameter name="EndDate"&gt;
                      &lt;value&gt;2016-07-01 00:00:00&lt;/value&gt;
                  &lt;/reportParameter&gt;
                  &lt;reportParameter name="ReportID"&gt;
                      &lt;value&gt;COD2345&lt;/value&gt;
                  &lt;/reportParameter&gt;
                  &lt;reportParameter name="client"&gt;
                      &lt;value&gt;Fetchr&lt;/value&gt;
                  &lt;/reportParameter&gt;
                  &lt;reportParameter name="manager"&gt;
                      &lt;value&gt;Zeeshan Asgar&lt;/value&gt;
                  &lt;/reportParameter&gt;
                  &lt;reportParameter name="client_add"&gt;
                      &lt;value&gt;Near Bhagat Singh Park, Malviya Nagar, New Delhi-110017&lt;/value&gt;
                  &lt;/reportParameter&gt;
              &lt;/parameters&gt;
          &lt;/reportExecutionRequest&gt;
          """
          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
  </code></pre>
  
  <p>Help.</p>
  
  <p>Thanks in advance.</p>