Error 400 creating a form

Solved!
Posted in General by Francisco Espada Mon Mar 21 2016 22:32:17 GMT+0000 (Coordinated Universal Time)·7·Viewed 2,040 times

function prueba(){ var headers = { 'Authorization': 'Basic ********************************' }; var payload = { 'name': 'prueba1', 'templateId': '**********************************', 'assignment': { 'id': '************************************', 'type': "User" }, 'fields': {} } var params = { 'contentType': 'application/json; charset=utf-8', "headers": headers, "method": 'post', "payload": payload } var url = 'https://api.goformz.com:443/v2/formz'; var response = UrlFetchApp.fetch(url, params); var responseCode = response.getResponseCode(); } *************** ERROR 400 ******************** "code": 400, "status": "Bad Request", "message": "Invalid Model", "errors": null, "requestUrl": "https://api.goformz.com/v2/formz"... If I use the following model in "Version 2 of the GoFormz Rest API" web page, it works fine, but it doesn't work in google apps scripts. { 'name': 'prueba1', 'templateId': '**********************************', 'assignment': { 'id': '************************************', 'type': "User" }, 'fields': {} }
Mar 21, 2016

Are you sure that the payload JSON is valid? That error usually occurs when the api can't parse the JSON.

I noticed that there is a missing quote after the id property value: 'id': '**,. That might've been removed when you replaced the id with asterisks though. An easy way to validate your JSON would be to use an online parser tool like this: http://jsonparseronline.com/

Mar 22, 2016

I've parsed parsed the payload and is correct, even when I copy and paste into de web api tool, it works.

Before the ERROR 400, it showed ERROR 500

I had to insert: 'contentType': 'application/json; charset=utf-8'

And I solved the ERROR 500, but now shows ERROR 400. Perhaps it could have something to do.

Mar 22, 2016

The problem seems to be that UrlFetchApp doesn't support nested objects in the payload. The 'assignment' property is coming through as null. The workaround would be to set the contentType parameter to 'application/json; charset=utf-8' like you did, and also serialize the payload as JSON.

Your params object should look something like this:

var params = {
    'contentType': 'application/json; charset=utf-8',
    "headers": headers,
    "method": 'post',
    "payload": JSON.stringify(payload)
  }
Mar 22, 2016

Works great!

Thanks again.


marked this as solved
July 3, 2018

Please validate your json using https://jsonformatter.org/json-parser

July 4, 2018

I believe that your JSON is not valid you can validate your JSON by any inbuilt function or you can also validate by it online
Like
http://www.mycodemyway.com/beautifiers/json-parser

Markdown is allowed