Error 400



  • I installed a jsreport instance on ubuntu as is in your site to generate PDF for my application.
    On my application i make requests to /api/report with the data to generate those PDF, but my application gives HTTP error 400, and on jsreport log it shows:

    2020-06-29T09:02:12.013Z - error: Error during processing request at http://localhost:5488/api/report, details: Unexpected token { in JSON at position 15642, stack: SyntaxError: Unexpected token { in JSON at position 15642
        at JSON.parse (<anonymous>)
        at parse (/home/prog3/Downloads/jsreportapp/node_modules/body-parser/lib/types/json.js:89:19)
        at /home/prog3/Downloads/jsreportapp/node_modules/body-parser/lib/read.js:121:18
        at invokeCallback (/home/prog3/Downloads/jsreportapp/node_modules/raw-body/index.js:224:16)
        at done (/home/prog3/Downloads/jsreportapp/node_modules/raw-body/index.js:213:7)
        at IncomingMessage.onEnd (/home/prog3/Downloads/jsreportapp/node_modules/raw-body/index.js:273:7)
        at emitNone (events.js:106:13)
        at IncomingMessage.emit (events.js:208:7)
        at endReadableNT (_stream_readable.js:1064:12)
        at _combinedTickCallback (internal/process/next_tick.js:138:11)
        at process._tickCallback (internal/process/next_tick.js:180:9)
    

    and the content of the POST (with the content type application/json) request that i sent to /api/render is:

    {
       "data":{
          "dataemissao":"29/6/2020",
          "empresa":"SOF",
          "filtradapor":"Código",
          "listagem":"Listagem",
          "ordenadapor":"",
          "terceiros":{
             "0":{
                "f1":"",
                "f2":"ccc"
             },
             "1":{
                "f1":"",
                "f2":"ccc"
             }
          }
       },
       "template":{
          "chrome":{
             "printBackground":"true"
          },
          "content":"<html> <head> <meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"> </head> <body style=\"font-size:11px;\"> <h1>{{empresa}}</h1> <h2>{{listagem}}</h2> <div id=\"divActivites\" name=\"divActivites\" style=\"width:100%; border:1px solid black;\"> Filtrada por: {{filtradapor}}<br> Ordenada por: {{ordenadapor}}<br> Data de emissão: {{dataemissao}} </div> <br> <table style=\"width:100%\"> <thead> <tr> <th>Código</th> <th>Nome</th> <th>Localidade</th> <th>Código postal</th> <th>País</th> </tr> </thead> <tbody> {{#each terceiros}} <tr> <th> {{f1}} </th> <th> {{f2}} </th> </tr> {{/each}} </tbody> </table> </body></html>",
          "engine":"handlebars",
          "phantom":{
             "footer":"<div>Pág. {#pageNum} de {#numPages}<br></div>",
             "footerHeight":"2cm"
          },
          "recipe":"phantom-pdf"
       }
    }
    

    PS: the original json was edited but is valid.



  • Likely a problem with your client call. The server receives an invalid json.
    Can you share the code you use to invoke the API call?
    Can you try to do the same in a postman or another rest API tool?
    What if ou try to do something very simple to limit the possible problems, like a post like this:

    POST /api/report
    HEADER content-type: application/json
    { "template": { "recipe": "html", "engine": "none" } }
    


  • If i do a POST through my app i get the 400 end the jsreport error.
    But if i post through postman and with exactly the same body the report is generated successfuly and without error.



  • This indicates the problem is in your app code doing the request.
    Now I see you mentioned you do request to /api/render, note it should be /api/report.



  • I'm doing the request to report, wrote it wrongly.
    It's strange because i had errors on html (it worked previously, i don't know why now those error make problems) that cause the PDF not rendered and correct it, now it generates the PDF correctly, but the error keeps apearing



  • Now doesn't work again, and both, json and html from template are valid


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.