message":"Could not parse report template, aren't you missing content type?



  • i try to get the jsreport studio template report in my nodejs application but i can not able to get the details through api while i trying i got this error "message":"Could not parse report template, aren't you missing content type? '''

    then i fount some help from here "https://groups.google.com/forum/#!searchin/jsreport/message"$3A"Could$20not$20parse$20report$20template$2C$20aren$27t$20you$20missing$20content$20type$3F|sort:relevance/jsreport/DMm02ZtwDLM/zhYUnr_tAAAJ"
    but
    it
    not
    working
    the
    code
    is
    data: JSON.stringify({
    "template": { "shortid" : "EkRWTuKil" },
    "data": { "aProperty": "value" }
    })



  • Hi,
    Just looking at the error message and the former answer of Jan Blaha and I can tell you're looking in the wrong direction.
    Your error doesn't come from the data attribut of your ajax request, but you're certainly missing a header attribute where you would define the content-type of your request.

    (here after the originale answer from Jan Blaha as in your link)

    $.ajax({
                type : "POST",
                url: 'http://localhost:3000/api/report',
                headers: { // this is were you should look into
                    'Content-Type' : 'application/json'
                },
                data: JSON.stringify({
                    "template": { "shortid" : "EkRWTuKil" },
                    "data": { "aProperty": "value" }
                })
    })
    

    In addition I invite you to look for some information regarding http headers link to wikipedia as it is a really important fact when working with request.



  • Sorry, I just cannot get past this error. Trying to call the api from a standalone .js file in node, using request module.
    I can get the call to work in PostMan. But in code I'm doing:

    options = {'method': 'POST',
               'url': jsr, //valid string to report api
               'headers': { 'User-Agent': 'request',
                            'Content-Type': 'application/json' },
               'data': JSON.stringify({ 'template': { 'shortid': reportId }, //valid short id
                         'data': req.data // a valid report data object (7751 chars)
                       })
              };
    request.post(options).pipe(fs.createWriteStream(reportTargetFile));
    

    whatever I try, I get {"message":"Could not parse report template, aren't you missing content type?"} as the contents of my file.
    What am I doing wrong????


  • administrators

    hi! you can take a look at the code in our node.js client for an example of a correct request.

    at first glance it seems that you should not pass 'method': 'POST' in options (because you are already describing the method when calling request.post), also the correct option for passing the json is body not data.

    options = {
               'url': jsr, //valid string to report api
               'headers': { 'User-Agent': 'request',
                            'Content-Type': 'application/json' },
               'body': JSON.stringify({ 'template': { 'shortid': reportId }, //valid short id
                         'data': req.data // a valid report data object (7751 chars)
                       })
              };
    

    try with that, it should work



  • Wow, with that - immediate success :)
    'method': 'POST' is indeed redundant in options (but doesn't appear to harm)
    'body' is indeed the trick that makes it work, note that the guidance in prior comment was the confusing bit as it suggests 'data' instead (which may however be a valid construct in a JQuery Ajax call).


  • administrators

    'method': 'POST' is indeed redundant in options (but doesn't appear to harm)

    yes, just a general recommendation 😄

    'body' is indeed the trick that makes it work, note that the guidance in prior comment was the confusing bit as it suggests 'data' instead (which may however be a valid construct in a JQuery Ajax call).

    yes, maybe you got confused by that. anyway, glad that it is working for you now. happy coding!



  • Is this example code still valid? I am getting the same error, here's my code:

    const request = require("request");
    
    const guid = require("guid");
    
    const express = require("express");
    const router = express.Router();
    const fs = require("fs");
    
    
    router.use(function (req, res, next) {
      // log each request to the console
      // continue doing what we were doing and go to the route
      next();
    });
    
    router.post("/report", async function (req, res) {
      let data = req.body.data;
      let path = "temp/report.pdf";
      const response = await fetchReport(path, data, res);
      console.log(response);
    });
    
    var fetchReport = async (path, data) => {
     let jsr = process.env.JSREPORTURL + "/api/report";
      let options = {
        'url': jsr, //valid string to report api
        'headers': { 'User-Agent': 'request',
        'Content-Type': 'application/json' },
        'body': JSON.stringify(data),
      };
      request.post(options).pipe(fs.createWriteStream(path));
     
    };
    
    module.exports = router;
    

  • administrators

    @Samerkassem the request options looks correct, maybe the data variable is not getting what you expect, what do you get when doing console.log(JSON.stringify(data))?



  • @bjrmatos {"to":"John Lennon","from":"George Harrison","price":800} which is the data example I sent from client.



  • @bjrmatos actually I have changed the whole code, it's working for the backend but i am getting another very weird issuer when sending to client, i will make a new topic


Log in to reply
 

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