2

Simple code and straightforward. It works with postman but fails with Apps Script.

function validateAddress () {

  const url = 'https://addressvalidation.googleapis.com/v1:validateAddress?key=';
  const apikey = '...';

  let payload, options, temp;

  payload = JSON.stringify({
    "address": {
      "addressLines": "1600 Amphitheatre Pkwy"
    }
  });

  options = {
    'muteHttpExceptions': true,
    'method': 'POST',
    'Content-Type': 'application/json',
    'body': payload
  }

  temp = UrlFetchApp.fetch(url + apikey, options);
  Logger.log(temp)

}

Error:

{
  "error": {
    "code": 400,
    "message": "Address is missing from request.",
    "status": "INVALID_ARGUMENT"
  }
}

EDIT:

Change options to

  options = {
    'muteHttpExceptions': true,
    'method': 'POST',
    'Content-Type': 'application/json',
    'payload': payload
  }

Gives error:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}\": Cannot bind query parameter. Field '{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}\": Cannot bind query parameter. Field '{\"address\":{\"addressLines\":\"1600 Amphitheatre Pkwy\"}}' could not be found in request message."
          }
        ]
      }
    ]
  }
}

Documentation: https://developers.google.com/maps/documentation/address-validation/requests-validate-address

2 Answers 2

3

From the documentation, it seems that addressLines is an array of string, not a string. Does that change something ? (I don't have a key, so I cannot try myself).

And more important, your options object is incorrect (check doc). It should be

options = {
    'muteHttpExceptions': true,
    'method': 'POST',
    'contentType': 'application/json',
    'payload': payload
  }
6
  • addressLines works on postman with array or string. "payload" gives different error.
    – sangnandar
    Commented Nov 14, 2022 at 12:35
  • please show the new error then
    – ValLeNain
    Commented Nov 14, 2022 at 12:42
  • I don't know why it works with Postman but still, try to declare an array for addressLines, it might be the issue
    – ValLeNain
    Commented Nov 14, 2022 at 13:02
  • Changed addressLines to array, error are the same (exactly the same).
    – sangnandar
    Commented Nov 14, 2022 at 13:16
  • 2
    Aahhh... fool me, I knew it must be a stupid mistake. Changing Content-Type to contentType actually solves the problem too. It's all clear now. Thank you.
    – sangnandar
    Commented Nov 14, 2022 at 13:39
1

I don't know why it works but changed options to

  options = {
    'muteHttpExceptions': true,
    'method': 'POST',
    'headers': {
      'Content-Type': 'application/json'
      },
    'payload': payload
  }

Solves the problem.

1
  • in the headers you can write Content-Type, in the object options(first level of properties) you can have contentType. But you can't put one for the other.
    – ValLeNain
    Commented Nov 14, 2022 at 13:34

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