Hello!  Thanks for posting your questions here.  

Your GET and POST handlers are doing very different things in this case so 
I would not expect to receive the same response from both.  

The 502 Bad Gateway is most likely caused by phantom.takeScreenShot(url, 
response) and there. I could not find any such method listed in the PhantomJS 
documentation <http://phantomjs.org/screen-capture.html>. I did however 
find the documentation for the webpage module which provides screen 
capturing functionalities.

In fact, there appear to be functions to accomplish exactly what you seek 
if I understood that correctly. The open(url, callback) 
<http://phantomjs.org/api/webpage/method/open.html> method issues the HTTP 
request and invokes the callback when done. The renderBase64(format) 
<http://phantomjs.org/api/webpage/method/render-base64.html> method renders 
the pages content in a given format and outputs the content in base64.

The example provided in their documentation is fairly concise. Here's one 
with some additional error handling:
// top of your program
var webpage = require('webpage');

// handler for your post request
// issues HTTP request to given URL
// returns base64-rendered screenshot on success
// or error message on failure
function post_handler(request, response) {
  var page = webpage.create();
  
  page.viewportSize = {
    'width': 1920,
    'height': 1080
  };
  
  page.open('https://www.google.com', function (status) {
    var base64;
    
    if (status === 'success') {
      base64 = page.renderBase64('PNG');
      return response.status(200).send({
        'screenshot': base64
      });
    }
    
    response.status(200).send({
      'error': 'Request to ' + page.url + ' returned with status ' + status
    });
  });
}


This seems to be the way their documentation suggests getting screen 
captures. Let me know if this works for you. If you still encounter errors, 
I would suggest adding listeners to onResourceError 
<http://phantomjs.org/api/webpage/handler/on-resource-error.html> and other 
error-related events.  If getting unknown errors beyond that, please post 
some of your request logs from your application as they might have more 
relevant information.

On Monday, May 30, 2016 at 9:42:45 PM UTC-4, Harris Robin Kalash wrote:
>
> So I deployed a nodeJS script on GAE and whenever I hit a POST endpoint I 
> get a 502 Bad Gateway error. The endpoint is a simple service that grabs a 
> screenshot of a page using phantomJS and returns a JSON that contains the 
> base64 representation of the image in it. 
>
> Doing a GET to this endpoint works fine and returns a healthy 200 
> response, however as soon as I try a POST request I get: **502 Bad Gateway**
>
> Here is my **app.yaml**:
>
>     service: pdf-service
>     
>     # [START runtime]
>     runtime: nodejs
>     vm: true
>     # [END runtime]
>     
>     threadsafe: yes
>     
>     # Temporary setting to keep gcloud from uploading node_modules
>     skip_files:
>      - ^node_modules$
>     
>     handlers:
>     - url: (.*)/
>       script: app.js
>       secure: always
>
>
> My **app.js** script:
>
>     'use strict';
>     
>     var express = require('express');
>     var app = express();
>     var cors = require('cors');
>     var bodyParser = require('body-parser')
>     var phantom = require('./phantom');
>     
>     app.use(cors());
>     // parse application/x-www-form-urlencoded
>     app.use(bodyParser.urlencoded({ extended: false }))
>     
>     var tmpURL = 'https://www.google.com';
>     // [START hello_world]
>     // Say hello!
>     app.post('/', function(req, res) {
>         console.log('requrl', req.body);
>         phantom.takeScreenShot(tmpURL, res);
>     });
>     
>     app.get('/', function(req, res) {
>       res.status(200).send({'greetings': 'Hello World'});
>     });
>     // [END hello_world]
>     
>     if (module === require.main) {
>         // [START server]
>         // Start the server
>         var server = app.listen(process.env.PORT || 8080, function() {
>             var host = server.address().address;
>             var port = server.address().port;
>     
>             console.log('App listening at http://%s:%s', host, port);
>         });
>         // [END server]
>     }
>     
>     module.exports = app;
>
>
> Notes:
> **The code works on my local environment.**
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/e7c877ca-1cdc-4384-a6ea-0bdd9c14c8fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to