When you set out to integrate with an API, you will often find cURL examples in the API’s documentation, like this one from box.com.
Or you use a tool like Postman, Insomnia, or Paw to test the API calls and then use the cURL syntax that these tools can automatically generate for you.
These cURL examples are meant to be used in command-line cURL calls (they start by invoking the curl executable as the first word in the syntax), and when you try to use the syntax as-is in FileMaker, it will fail. That is something we wrote about a few years ago. FileMaker uses a cURL library, not the cURL command line.
Translating the provided cURL examples into the components needed to make FileMaker’s Insert From URL script step work is fairly straightforward. To make things even easier, this blog post comes with a utility file that does it for you.
In FileMaker, you need to specify the URL and the cURL options separately. And you need to make sure that your cURL options do not contain any single quote characters (‘). Any regular quotes need to be escaped, of course, which can be a bit of pain.
FileMaker cURL Utility
In the utility, just paste in the cURL syntax that you get from your favorite tool or from the API documentation:
Click the button, and you will get a breakdown of the elements for your Insert From URL script step:
The tool will also separate any data that you are sending and show how you can use a variable and the odd “@$” syntax as a placeholder for the data or file that you are sending. The example above shows a JSON body that is being sent to the API. You would build the $payload variable ahead of time with the FileMaker JSON functions. That way, you never have to go through the trouble of escaping any quotes in the data you are transmitting.
When you are trying to send a file, the tool will alert you to the fact that Claris has deliberately neutered – for security reasons – the cURL library so that it cannot read from a file on the hard drive. You will need to put the file in a container field or variable first and then reference that field or variable in the cURL options syntax.
Moving Forward with FileMaker cURL
If you have any questions about how to translate your auto-generated cURL or would like help further customizing your FileMaker application, our team of consultants may be able to help. Contact us today.
Wim – excellent article and tool. Your commitment to the Claris community is legendary.
Thanks, Josh!
Very useful concept and file, will be useful in the community for those without the experience to do this task by hand…
A great idea, well executed
Thanks, John!
This is such a great idea. Love it. I know this will come in handy when I’m working with new API’s.
This is a very handy little utility – I just tested it with SendGrid, and it worked nicely to get started a little bit quicker.
There is a minor issue in the Substitute on line 37 of the script “parse” – you are removing all spaces from the payload, which is not correct, as it e.g. – in this case – also removes all spaces from the e-mail Subject and Content.
Thanks, David!
I’ll post a new version soon.
Hi – just come across this file and it works really well apart from the issue raised by David re line 57 (I am using version 3 so the line number is different but the problem is the same). I have tried to sort it out by removing a variety of numbers of spaces and it works but is not flexible enough to cope with anything that does not have those set number of spaces:
Substitute ( $payload ; [ “‘{” ; “{” ] ; [ “}'” ; “}” ] ; [ “: ” ; “:” ] ; [ Char ( 9 ) ; “” ] ; [ ” ” ; “” ] ; [ ” ” ; “” ] )
I feel sure there’s a better way – any ideas?
Thanks again
John
Hi John,
Can you email me an example of what you are trying to parse? I can then see about coming up with something generic.
Best regards,
Wim
Your explanation and your utility have been very helpful.
In your example, although I understand that $payload refers to the JSON object in the box printed below the boldfaced word $payload, I do not understand the references or meanings of either
1. option -D, following the variable $payload
2. the variable $header
If you could explain the meaning of both, I would be greatly appreciative.
The -D $header entry is there for FileMaker to dump the response headers into. Those response headers are crucial in your script to determine if the API call was successful or not. Here’s an example of a set of response headers:
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Date: Mon, 14 Nov 2022 08:03:02 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 505
Connection: keep-alive
You’ll almost always want to know what the http response code was (200 in this case).
The syntax is a bit odd because we’re not giving the cURL command what is currently in $header. In fact $header doesn’t exist at this point but the cURL command will set that variable with whatever response headers were returned.
Wim, I greatly appreciate your great explanations on constructing curl syntax using a data variable, and your explanations on the matter in which the header variable, created during the FMP’s insert from cURL process, is deletable. Thank you so much for your great help.
Wim
doesn’t cope with https urls, needs the parse script amending…
Thanks, John!
Turns out that example you were working on had non-space whitespace between the parts of the cURL string, v3 of the demo file takes care of that.
Hi, please could you advise how I would build the $payload variable ahead of time with the FileMaker JSON functions? Would I use JSONGetElement? I get errors stating that the table cannot be found. I use the data produced by the Curl tool on this page. TIA 🙂
You would use the JsonSetElement() function to build your json payload.
https://help.claris.com/en/pro-help/content/jsonsetelement.html?Highlight=jsonsetelement
This may help: https://www.youtube.com/watch?v=S89sYkWBcUY
Wow!
Thank you very much for the article an especially the file. Very helpful indeed 🙂
Thank you Wim. This utility file helps me a lot to configure my first API request with Filemaker. I spent hours without success when I discover your magical file.
When I use the utility it provides some output but the file appears to freeze / crash so I have to crash out of FM. Am I doing something wrong?
It shouldn’t crash so I would suggest downloading a fresh copy and trying with that. Perhaps something got damaged in the download.