Send POST request from a button

#1

Hi,

Wondering if it’s possible to send an arbitrary POST request over HTTP from a button in Coda? I have a few ideas for Microsoft Flow I’d like to implement, but not sure how best to trigger them.

Joe

3 Likes

#2

I don’t think it’s possible to do this directly, but there are certainly a couple of options. The first would be to use a webhook and button with a URL option.

The second would be Zapier or similar services.

1 Like

#3

Thanks Murray. I just wrote a quick microservice with now.sh to forward my ‘gets’ as ‘puts’, and respond with some window closing script and it seems to work, but is a bit ‘hacky’.

Joe

0 Likes

#4

Can you say a bit more about what you did here? It sounds cool. I’m also looking for ways to increase communication between Coda and the outside world, and I saw someone recently had a related question about reacting to row changes (a case that the Zapier integration currently doesn’t cover)

0 Likes

#5

(Hi Nick, I’m not sure about your level of technical knowledge around APIs and Javascript and HTTP calls and stuff so let me know if you’ve been able to follow or whether I need to go into a bit more detail).

Basically I wrote a quick bit of javascript which just receives a ‘GET’ request and makes a ‘POST’ request.

If the ‘GET’ request contains a URL as a query parameter, then it’ll attempt to format the whole query string as JSON and POST it to the ‘url’ it received.

Once it’s 'POST’ed, it’ll either send a piece of JS which will close the window, or write the error to window.

It needs some tweaking because right now, certain specific parameters which I need to pass on to my API endpoint in the URL (eg: API token) are manually pulled out of the query string and added to the URL to post to. In the next iteration, I’ll accept a few different params: url, data, and headers.

Anyway, long story short, right now, if I ‘GET’ https://fake-address.now.sh/?url=api.other-fake-address.com&firstname=Joe&lastname=Innes&apitoken=1234, then the lambda function will ‘POST’ to api.other-fake-address.com/?apitoken=1234 with a JSON payload of

{
  "firstname": "Joe",
  "lastname": "Innes"
}

One easy way to make a ‘GET’ request is to try to open the resource in a browser (which you can do using the ‘Open Hyperlink’ button action).

Now, when you use the ‘Open Hyperlink’ button action, a new window opens up. To avoid this window getting in the way, my lambda function returns either the error, or <script>window.open('', '_self', ''); window.close();</script>, which causes the window to close itself again. It’s a bit ugly, but it works.

Once I’ve implemented the proper way of handling the payload/url separation I’ll make my code public along with deployment instructions so that anyone else who’s interested can spin up their own copy (hopefully either today or tomorrow).

2 Likes

#6

Will be interested to see your process here, I think it might help me with my Integromat integrations :+1:

0 Likes

#7

So, the code is as follows:

const axios = require('axios')
const querystring = require('querystring')
const { parse } = require('url')

module.exports = async (req, res) => {
  console.log('Starting... received ' + req)
  const { query } = parse(req.url, true)
  const data = query.data || null
  const headers = query.headers || null
  
  let url = query.url || null

  if (!url) {
    res.end('You must specify a forwarding url')
  }

  let newQuery = query

  delete newQuery.data
  delete newQuery.headers
  delete newQuery.url

  let sep = Object.keys(newQuery).length > 0 ? "&" : "/?"

  url = url + sep + querystring.stringify(newQuery)

  await axios.post(url, data)
  .then(function (response) {
    console.log(response)
  })
  .catch(function (err) {
  	res.end(error)
  })

  res.end(`<script>window.open('','_self').close()</script>`)
  
}

I’m deploying with now (zeit.co/now), which guides you through the account setup, and lets you deploy to the with a single command.

Code also on https://github.com/joeinnes/sortingoffice, along with config file for Now

4 Likes