Daewizal Forshizal
Daewizal Forshizal

Reputation: 141

Rest API cURL POST via Google Script FetchUrlApp()

I’m trying to cURL post to Parse platform via Google Script. Tried different combinations, but always get response 400 from server. Does anyone know where the problem is in the code? Thank you all in advance!

function myFunction() {
  var payload = JSON.stringify({
    "score": 1337,
    "playerName": 'Sean Plott',
    "cheatMode": false
});

var headers = {
    "X-Parse-Application-Id": 'myid',
    "X-Parse-Master-Key": 'mykey',
    "X-Parse-Url": 'https://parseapi.back4app.com/',
};

var options = {
    'method': 'post',
    'payload': payload,
    'Content-Type': 'application/json',
    //muteHttpExceptions : true,
    'headers': headers
};
var test = UrlFetchApp.fetch('https://parseapi.back4app.com/classes/GameScore', options);
Logger.log(test);

Upvotes: 0

Views: 1913

Answers (2)

TheAddonDepot
TheAddonDepot

Reputation: 8974

In your options object replace Content-Type with contentType.

Upvotes: 1

Hugo
Hugo

Reputation: 637

It looks like you are using the UrlFetchApp.fetch() API correctly.

Note that 400 errors mean that

The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

HTTP Statuses

In other words, your request is probably malformed. A possible cause is that you are using incorrect headers or not supplying enough information with your request.

I don't have experience with the Parse platform, but it looks like it expects a X-Parse-REST-API-Key header based on this documentation.

Upvotes: 0

Related Questions