POST Request with pointer from Google Script

Hi Everyone,

I am currently working for a non profit organization on an integration between my parse server (hosted on AWS) and a Google spreadsheet. I am trying to POST new rows in classes using the google script of the google spreadsheet.

So far I succeded to:

  • make GET request to from parse and collect the results.
  • makePOST request to parse and add new items in a parse class as long as I don’t specify any date or any pointer.

The following POST resquest is working fine:

//Define the API KEY and APP ID
var API_KEY = 'blablabla';
var applicationId ='blablabla'

function InsertIntoParse() {
  //Define URL
  var root = 'http://blablbla.amazonaws.com:80/parse';
  var endpoint = '/classes/JEPT_ENFANT';
  
  //Define parameters
  var params = {
    "method": "POST",
    "muteHttpExceptions": true,
    "Content-Type": "application/json",
    "headers": {
      "X-Parse-Application-Id": applicationId,
      "X-Parse-REST-API-Key": API_KEY
    },
    "payload":{
      "enf_prenom": "John",
      "enf_sexe": "Homme"
    }
  };
  //Send Request
  var response = UrlFetchApp.fetch(root+endpoint, params);
  Logger.log('SK PARSE='+response);
}

If I add the pointer on the fam_cd column:

"payload":{
  "enf_prenom": "John",
  "enf_sexe": "Homme",
  "fam_cd":{"__type":"Pointer","className":"JETP_FAMILLE","objectId":"n4qkgYC0Qg"}
}

The script won’t work and return the following message:

{"code":111,"error":"schema mismatch for JEPT_ENFANT.fam_cd; expected Pointer<JETP_FAMILLE> but got String"}

Please note that the same payload used on the parse dashboard API Console work fine. :thinking:

I have also tried the following possibilities:

"fam_cd":"n4qkgYC0Qg"
    
"fam_cd": {"__op":"AddRelation","objects":[{"__type":"Pointer","className":"JETP_FAMILLE","objectId":"n4qkgYC0Qg"}]}

I have a similar issue with the Date columns. The follwing payload :

"payload":{
  "enf_prenom": "John",
  "enf_sexe": "Homme",
  "enf_ddn": {"__type": "Date", "iso": "2018-04-18T01:00:00.000Z"}
}

Returns the follwing error message:

{"code":111,"error":"schema mismatch for JEPT_ENFANT.enf_ddn; expected Date but got String"}

I have also tried the following possibilities :

"enf_ddn": {"__type": "Date", "iso": "2018-04-18T01:00:00.000"}
    
"enf_ddn": {"__type": "Date", "iso": "2018-04-18T01:00:00Z"}
    
"enf_ddn": {"__type": "Date", "iso": "2018-04-18'T'01:00:00.000'Z'"}
    
"enf_ddn": {"__type": "Date", "iso": "2018-04-18"}
    
"enf_ddn": "2018-04-18"
    
"enf_ddn": {"__type": "Date", "iso": new Date(2018,04,18)}

var DateBirth = Utilities.formatDate(new Date(2018,03,18), "GMT+1", "yyyy-MM-dd'T'HH:mm:ss'Z'")
"enf_ddn": {"__type": "Date", "iso": DateBirth}

Clearly there is an issue in the way I send the information to the REST API but I can’t understand why it works with Strings and with the API Console of the Dashboard and it’s not working for Pointers and Dates!

I’m open and thankful for any idea you will suggest. Please note that I’m not that skilled in the use of APIs and in hte use of Parse platform.

Thanks a lot!
Slim

Maybe Google Script does some “stringification” on the nested object sent?

You should try to add a beforeSave hook on your class and see how the payload looks once it reaches Parse.

Thanks a lot codeKonami !
I don’t have any expirience in WebHooks but I’ll figure it out with the documentation !
I come back to you as soon as I have some updates !

In the meanwhile if anyone else have some idea about what’s happening, I’m still listening. :smile:

Hi All,

I had some help from a friend of mine. I wasn’t able to find the solution simply because I was not looking at the right place :persevere:

Actually my issue comes from the Content-type declaration in this command:

var params = {
  "method": "POST",
  "muteHttpExceptions": true,
  "Content-Type": "application/json",
  "headers": {
      "X-Parse-Application-Id": applicationId,
      "X-Parse-REST-API-Key": API_KEY
  },
  "payload":{
      "enf_prenom": "John",
      "enf_sexe": "Homme"
  }
};

Content type should be in the header. So the correct command is:

var params = {
  "method": Method,
  "muteHttpExceptions": true,

  "headers": {
    "Content-Type": "application/json",
    "X-Parse-Application-Id": applicationId,
    "X-Parse-REST-API-Key": API_KEY
  },
  "payload":JSON.stringify({
    "enf_prenom": "Toudou",
    "enf_sexe": "Homme",
    "fam_cd": {
      "__type": "Pointer",
      "className": "JETP_FAMILLE",
      "objectId": "n4qkgYC0Qg"
    },
    "enf_ddn": {
      "__type": "Date",
      "iso": "2018-04-18T01:00:00.000Z"
    }
  })
};

My understanding is that when the Content-Type is not in the header, it is completely ignored (no surprise here) and the Parse Server considers a different type which works for string but won’t work for any other object. Too bad there is no warning sent about this!

One last thing: Please note the use of the “stringify” function to transform the params into string.

Thank you @codeKonami.