i need to call my web API post method using require('request')



  • I am using JSreport Studio

    HTML Code

    <!--
    Custom server side script is used to fetch data from the remote REST API server,
    handlebars to render html and phantom-pdf recipe to convert html into pdf.
    The template also uses helper function toJSON defined in global helpers asset.

    It requires allowed http module in scripts in the configuration file:
    "scripts": { "allowedModules": ["http"] }
    !-->
    <html>
    <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <link rel="stylesheet" href="https://cdn.rawgit.com/olton/Metro-UI-CSS/master/build/css/metro.min.css">
    <script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.min.js'></script>
    </head>
    <body>
    <h1>{{title}}</h1>

    <canvas id='Doctors' style="margin-top:30px"></canvas>
    <table class="table striped">
        <thead>
            <tr>
                <th>Outstanding</th>
                <th>DoctorID</th>
                <th>DoctorLName</th>
                <th>DoctorName</th>
        
            </tr>
        </thead>
        <tbody>
            {{#each doctors}}
            <tr>
                <td>{{Outstanding}}</td>
                <td>{{BranchId}}</td>
                <td>{{BranchName}}</td>
                <td>{{OutstandingPatient}}</td>
                
            </tr>
            {{/each}}
        </tbody>
    </table>
    
    <script>     
        var data = {{{toJSON this}}}
    

    </script>
    </body>
    </html>

    the script code

    function beforeRender(req, res, done) {
    // the report parameter country can be send from the client API request
    req.data.title = req.data.title || 'Doc'

    require('request')({

      url:"http://localhost:7458/api/Report/GetdailyTransaction",
      method: "POST",
      json:true,
     headers: {'User-Agent':'Super Agent/0.0.1','content-type': 'application/json','Content-Length': 92},
      body: JSON.stringify({"From_Date":"2017-01-23T18:25:43.511Z","To_Date":"2017-06-07T18:25:43.511Z","BranchID":[1]})
    }, function(err, res, body){
       //request.template.data.orders = json.value;
    
       req.data.doctors = body;
    
        done();
    });
    

    }

    no error but no data showing in HTML, I test the api from fiddler return result and also i test the above code with Get method working fine


  • administrators

    maybe there is an error when calling your http endpoint, try to handle the error in the request call, maybe it will print an error in the console.

    function beforeRender(req, res, done) {
    // the report parameter country can be send from the client API request
    req.data.title = req.data.title || 'Doc'
    
    require('request')({
      url:"http://localhost:7458/api/Report/GetdailyTransaction",
      method: "POST",
      json:true,
     headers: {'User-Agent':'Super Agent/0.0.1','content-type': 'application/json','Content-Length': 92},
      body: JSON.stringify({"From_Date":"2017-01-23T18:25:43.511Z","To_Date":"2017-06-07T18:25:43.511Z","BranchID":[1]})
    }, function(err, res, body){
       //request.template.data.orders = json.value;
       
       if (err) {
         return done(err);
       }
    
       req.data.doctors = body;
    
        done();
    });
    

    also, are you sure that your http endpoint expects a POST request? should not be a GET request? maybe that is problem.



  • hi bjrmatos,
    thanks for your support, this is post method and I will try it and feed you



  • thanks it working fine


Log in to reply
 

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