-5

I have a script processing some refund payments that does not work anymore since approx. Feb 10th. Server responds Content-Type is not supported ... even if my header Content-Type looks actually correct

Same code worked before that date which makes me believe something was changed on the server side but I haven't notice anything in the square-connect API changelog recently that would explain this problem.

Any help would be much appreciated :) Thanks

See below JSON request and response.

{"method":"POST",
  "headers":
    {"Authorization":"Bearer [mypersonalid]",
     "Accept":"application/json",
     "Content-Type":"application/json"},
     "payload":
       {"payment_id":"[paymentid]",
        "type":"FULL",
        "reason":"reason"
       }
    }"
}


{"type":"bad_request",
 "message":"The Content-Type of this request is not supported. 
            Supported     type(s): application/json"
}
Olivier D
  • 1
  • 1

2 Answers2

0

Since you're using Google App Script, it looks like you have to set the Content-Type header using the special contentType parameter (docs here). That way, Google Apps Script knows to serialize the payload as JSON rather than multipart form data. If you just specify it as a header, Google will override it.

Troy
  • 1,599
  • 14
  • 28
0

Thanks a lot your comments. It's solved!

I've added the contentType in the params and it did not worked but I started to get a new error saying 'bad json object'. Si, I just searched for that and found that the actual encoding of the 'payload' object needed to be "stringify". See answer from user1021710 : https://stackoverflow.com/a/18851043/4615977 ("Google Apps Script UrlFetchApp with JSON Payload")

So in this case, contentType was necessary then encoding of the payload too. Google changed their own API around mid-Feb apparently, because code was the same and worked.

Thanks folks !!!

Sample basic code to POST refund request using google app script:

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

var payload = {
   "payment_id" : paymentid,
   "type" : type,  
   "reason" : reason,
};

var params = {
  "contentType": "application/json",
  "headers" : headers,
  "method" : "post",
  "payload" : JSON.stringify(payload),
};

var url = "https://connect.squareup.com/v1/me/refunds";
paymentsFetch = UrlFetchApp.fetch(url, params);
Community
  • 1
  • 1
Olivier D
  • 1
  • 1