question

Justin Heasman avatar image
Justin Heasman asked

Best Way to Retrieve Data from a Web Page in Cloud Script

I have some data that I need to retrieve from a web page in Cloud Script. What is the best way of doing this. I have tried doing it via a XMLHttpRequest, but the response is always returning nothing.

	var url = "http://www.google.com";
	var method = "POST";
	var postData = "Some data";
	var async = true;
	
	var request = new XMLHttpRequest();
	
	log.info("CLOUD SCRIPT: Before Function");  
	
	request.onload = function () 
	{
		var status = request.status;
		var data = request.responseText;




		log.info(" RESULT STATUS:  " + request.status);	
		log.info(" RESULT DATA:  " + request.responseText);	


		var ReturnResults = request.responseText;




		return ReturnResults;
	}
	
	log.info("CLOUD SCRIPT: After Function");  
	
	request.open(method, url, async);
	request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
	request.send(postData);




	log.info("CLOUD SCRIPT: End of Call");  
CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Joshua Strunk avatar image
Joshua Strunk answered

Can you post the logs? Also might consider just doing a

log.info("Request return: ", request);

//or

log.info("Request return: " + JSON.stringify(request));
  1. I have never seen an example http requests from Cloud Script use XMLHttpRequest so not sure on that. Generally they use the integrated http.request. Here is a slide example.
  2. When you assign the XMLHttpRequest.onload a function it does not actually call it. All it does is save it to get called when your request returns.
  3. I am pretty sure you should not use Async calls in Cloud Script. If this executes the way I imagine your handler function returns before you get the async response.

These are my best guesses I have never worked with external requests in Cloud Script, but I am hoping these give you a place to start looking for why your not getting expected results.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Justin Heasman avatar image
Justin Heasman answered

Perfect @Joshua Strunk. I was thinking the same about Async calls. Thanks so much :)

Justin

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.