admin管理员组

文章数量:1326134

I'm trying to fetch data from a graphql API through apps script.

There is a question using POST to fetch graphql data (Providing query string to fetch from a GraphQL API), but the url I need to fetch only allows GET.

The code I wrote is below:

function main() {
  var ss = SpreadsheetApp.getActiveSheet();
  url = "={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0"

  var response = UrlFetchApp.fetch(url,{method: 'GET', headers: { 'Content-Type': 'application/json'} })

  var lists = JSON.parse((response.getContentText()));

  Logger.log(lists);
}

If I paste the url into a browser all works fine:

{
  data: {
    answer: [
      {
        answer: {
          nomeDaEmpresa107176: "LAVORO",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 12341234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202279",
          createdAt: "2019-05-10T00:14:54+00:00",
          createdAtDevice: "2019-05-10T00:14:54+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "RURAL",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 1234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202280",
          createdAt: "2019-05-10T00:15:15+00:00",
          createdAtDevice: "2019-05-10T00:15:15+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "PRIMAVERA MÁQUINAS",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 12341234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202281",
          createdAt: "2019-05-10T00:15:51+00:00",
          createdAtDevice: "2019-05-10T00:15:51+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "SINAGRO",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 1234,
          telefone107179: "(66) 98412-1030"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202633",
          createdAt: "2019-05-10T20:54:02+00:00",
          createdAtDevice: "2019-05-10T20:54:02+00:00",
          createdAtCoordinates: {
            longitude: -52.20629,
            latitude: -12.59418
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      }
    ]
  }
}

But using Url Fetch in apps script I got the error:

"Invalid Argument: ={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0 (line 5, file "Código")"

What do I have to do for my code to work?

I'm trying to fetch data from a graphql API through apps script.

There is a question using POST to fetch graphql data (Providing query string to fetch from a GraphQL API), but the url I need to fetch only allows GET.

The code I wrote is below:

function main() {
  var ss = SpreadsheetApp.getActiveSheet();
  url = "https://coletum./api/graphql?query={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0"

  var response = UrlFetchApp.fetch(url,{method: 'GET', headers: { 'Content-Type': 'application/json'} })

  var lists = JSON.parse((response.getContentText()));

  Logger.log(lists);
}

If I paste the url into a browser all works fine:

{
  data: {
    answer: [
      {
        answer: {
          nomeDaEmpresa107176: "LAVORO",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 12341234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202279",
          createdAt: "2019-05-10T00:14:54+00:00",
          createdAtDevice: "2019-05-10T00:14:54+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "RURAL",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 1234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202280",
          createdAt: "2019-05-10T00:15:15+00:00",
          createdAtDevice: "2019-05-10T00:15:15+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "PRIMAVERA MÁQUINAS",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 12341234,
          telefone107179: "(66) 1234-1234"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202281",
          createdAt: "2019-05-10T00:15:51+00:00",
          createdAtDevice: "2019-05-10T00:15:51+00:00",
          createdAtCoordinates: {
            longitude: -52.20797,
            latitude: -12.58536
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      },
      {
        answer: {
          nomeDaEmpresa107176: "SINAGRO",
          cnpj107177: "26.693.198/0001-20",
          inscricaoEstadual107178: 1234,
          telefone107179: "(66) 98412-1030"
        },
        metaData: {
          userId: 9938,
          userName: "Mendes",
          source: "web_private",
          friendlyId: "1.202633",
          createdAt: "2019-05-10T20:54:02+00:00",
          createdAtDevice: "2019-05-10T20:54:02+00:00",
          createdAtCoordinates: {
            longitude: -52.20629,
            latitude: -12.59418
          },
          updatedAt: null,
          updatedAtCoordinates: {
            longitude: null,
            latitude: null
          }
        }
      }
    ]
  }
}

But using Url Fetch in apps script I got the error:

"Invalid Argument: https://coletum./api/graphql?query={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0 (line 5, file "Código")"

What do I have to do for my code to work?

Share Improve this question edited Jun 11, 2019 at 3:03 R.J. Dunnill 2,0893 gold badges11 silver badges23 bronze badges asked Jun 11, 2019 at 2:41 Guilherme Henrique MendesGuilherme Henrique Mendes 5185 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The solution was to ENCODE de url before fetch:

function main() {
  var ss = SpreadsheetApp.getActiveSheet();
  url_orcamentos = "https://coletum./api/graphql?query={answer(formId:7787){answer{nomeDaEmpresa107176,cnpj107177,inscricaoEstadual107178,telefone107179},metaData{userId,userName,source,friendlyId,createdAt,createdAtDevice,createdAtCoordinates,updatedAt,updatedAtCoordinates}}}&token=k4foipsju5ckosk4w8o0w084cc8oos0"
  url = encodeURI(url_orcamentos)
  var response = UrlFetchApp.fetch(url,{method: 'GET', headers: { 'Content-Type': 'application/json'} })

  var lists = JSON.parse((response.getContentText()));

  Logger.log(lists);
}

本文标签: javascriptIs there a way to fetch data from a graphql API in apps scriptStack Overflow