Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We have a webservice that returns product information. The service expects a JSON array in the post data... The

A sample script from a simple HTML test web page is as follows (this works as expected):

    <script; src="//code.jquery.com/jquery-1.11.2.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

    // request is array of product IDs e.g. pid:[71,72,74]

    // request is array of search terms e.g. find:['apples','oranges','pears']
    // request can contain requested fields from products
    // fields:['CostPrice','Published','title','Price']

    jQuery.ajax({
        url : 'http://somewebsite.com/__getProductData',
        type: 'POST',
        dataType : 'json',
        data: { pid:[658,71,6,700], fields:['pid','CostPrice','type','published','title'] },

    success:function(data) { 

        // do something with the response...    

        });


     },

    error: function(  jqXHR,  textStatus, errorThrown) {alert('Error Status:' + textStatus + '    Error:'+errorThrown); }

    }

    );


    });

    </script>

The web service, written in PHP, receives this correctly. Dumping (print_r) the data received by the web service from the client results in the following array:


    Array ( 
    [pid] => Array ( [0] => 658 [1] => 71 [2] => 6 [3] => 700 ) 

    [fields] => Array ( [0] => pid [1] => CostPrice [2] => type [3] => type [4] => published [5] => title ) 

Now to the problem.... I'm trying to call the webservice from a Google Sheet Script as follows:

    function getProduct( pid,datetime) {

      // 
      var url = 'https://somewebsite.com/__getProductData';

      //pid:[658,71,6,700], fields:['pid','CostPrice','type','published','title']

      var nids = new Array( 658,71,6,700 );

      var fields = ['pid','CostPrice','type','published','title']
      var payload =
          { 
          pid : nids,
          fields: fields

       };

       var options =
       {
         "method": 'POST',

         "payload": payload


       };

      var response = UrlFetchApp.fetch(url, options);

     Logger.log(response);


    }

When the PHP web service is called from Google Sheets script the JSON array is not receive correctly:

    Array ( [data] => { pid=[Ljava.lang.Object;@1bfbb500} fields=[Ljava.lang.Object;@3ffbb500})

I expect this is something associated with encoding or headers... I've tried most combinations of headers and contentType but to no avail.... e.g.

    var headers = { "Accept":"json", 
              "Content-Type":"application/json", 
             };

Suggestions anyone?

share|improve this question
    
As a workaround, since you have access to the PHP code, instead of serving a JSON, serve a HTML, and request with URL GET parameters. –  Kriggs Jan 11 at 14:33

2 Answers 2

Jonathan - your suggested comment is the solution....

This didn't work:

var options =
 {
 "method": 'POST',
 "payload": JSON.stringify(payload)
 };

but stringify on the payload array elements worked (note that simple elements worked without stringify e.g. 'text' : "some text here") :

 var nids = [658,71,6,700 ];
 var fields = ['pid','CostPrice','type','published','title']
 var payload =
      { 
      pid : JSON.stringify(nids),
      fields: JSON.stringify(fields)
   };
var options =
 {
 "method": 'POST',
 "payload": payload
 };
share|improve this answer
var options = {
  "method":  'POST', 
  "payload": JSON.stringify(payload)
};

Or stringify each of the values for pid & fields.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.