10 Replies Latest reply on Oct 21, 2016 10:15 AM by danjamins

    Posting JSON to RESTful API

    danjamins

      Hi,

       

      I have a project I'm working on to submit data from my FM14 customer database to a RESTful API using JSON.

       

      It seems FileMaker doesn't have much support for JSON without going to external plugins.

       

      I've tried using the Base Elements plugin and using the BE_JSON_Encode() function to get the JSON formatted. The only question I have now is how in the world do I POST this data to the URL I have.

       

      I can't figure this out and Google isn't producing much for results.

       

      I have a URL and two headers for content-type and authentication. I tried to write a script to do this but I have no idea how to go about setting this up.

       

      Please help!

       

      Thank you,

      Dan.

        • 1. Re: Posting JSON to RESTful API
          monkeybreadsoftware

          If you consider using MBS Plugin, you can read the examples on this page:

           

          http://www.mbsplugins.eu/CURLPerform.shtml

          • 2. Re: Posting JSON to RESTful API
            danjamins

            Thanks Christian, that was exactly what I needed. I couldn't pull that page up from Google and the documentation on MBS site had about 200 CURL functions to go through I couldn't find the right one. Thank you!

            • 3. Re: Posting JSON to RESTful API
              ch0c0halic

              Dan,

               

              Christian gave you the fishing pole. I'll try and give you some bait to use.

               

              First I've done this for Jive, which uses the REST API with json data documents. I made the commands data driven and the script is able to set specific cURL options as required for each transmission type (POST, PUT, DELETE, GET).

               

              The way I started was by making a table of the REST commands I wanted to use. Included were fields for the json formatted data and the transmission type. As you can see in the cJive_URL calculation several of the fields are used to define each segment needed for the REST command URL.

               

              I used these fields. The REST starts by building the cURL from the other fields in order to define the REST command and end-point. I use a substitute to replace a variable with the real data needed in the URL.

               

              January 25, 2016 13:01:13 Jive_7_processing.fmp12

              table_name:  Jive7_rest_commands

              Field Name Field Type Formula / Entry Option

              cJive_URL Calculation (Text) Unstored, =

              Jive_base_URL_g

              & Case ( IsEmpty

              ( jive_service ) ; "" ;

              jive_service & "/" )

              & Case ( IsEmpty

              ( jive_resource ) ; "" ;

              jive_resource & "/" )

              & Case (

              IsEmpty ( jive_command ) ;

              "" ;

              jive_command

              )

              cservice_descr Calculation (Text) Indexed, = " - " & jive_service & jive_description

              data_prefix Text Indexed

              id_jive_g Text Global

              jive_api_version Number Indexed

              Jive_base_URL_g Text Global

              jive_command Text Indexed

              jive_data_template Text Indexed

              jive_description Text Indexed

              Jive_password_g Text Global

              jive_required_data Text Indexed

              jive_resource Text Indexed

              jive_rest_command Text Indexed

              jive_rest_command_postsequence Text Indexed

              jive_rest_command_presequence Text Indexed

              jive_service Text Indexed

              Jive_username_g Text Global

              php_curl_send_type Text Indexed

              php_result_g Text Global

              z_cr_account Text Auto-enter: “Creation Account Name”

              z_cr_id Calculation (Number) = "jive." & z_cr_sn

              z_cr_sn Number Serial Number on creation with Current Value: “1000000275” Increment: “1”

              z_cr_timestamp Timestamp Auto-enter: “Creation Timestamp (Date and Time)”

              z_cr_user Text Auto-enter: “Creation Name”

              z_mod_account Text Auto-enter: “Modification Account Name”

              z_mod_timestamp Timestamp Auto-enter: “Modification Timestamp (Date and Time)”

              z_mod_user Text Auto-enter: “Modification Name”

               

              In order to create the json I did a GET for the data I needed for any PUT or POST command. This provided the template used in the json_data_template field. I then used substitution of real data for keywords in the template. For example <<username>> would be substituted out for the real username.

               

              The MBS code for the cURL command is a little tricky in that you must combine many pieces to get it to work. The data for the POST is required in a special command.

              First you need the header:  MBS("CURL.SetOptionHTTPHeader"; $curl; "Content-Type: application/json")

              Then the data:  MBS("CURL.SetOptionPostFields"; $curl; $d ) - Where $d is the json data packet.

              Then send the real type:  MBS("CURL.SetOptionPost"; $curl; 1)

              Then the custom data type:  MBS("CURL.SetOptionCustomRequest"; $curl; "POST")

               

              In my case just using the CURL.SetOptionPost transmission type is was not enough. It required the CURL.SetOptionCustomRequest parameter as well.

               

              The json template looks like this:

              {

                "content" : {

                  "text" : "<<$$docContent.content.text>>"

              },

                "parent" : "<<$$docContent.parentPlace.url.id>>",

                "subject" : "<<$$docContent.subject>>",

                "categories" : [<<$$docContent.categories>>],

                "tags" : [<<$$docContent.tags_new>>],

              <<$$docContent.question>>  "type" : "<<$$docContent.type>>"

              }

               

              The global variables are set in the script and the final output data ($d in the options) is set using a substitute to change them to real data.

               

              One more thing. HTTPS can often require authentication you can't support. Setting verify peer and host to off may allow your code to work. At least until you can get and install local certificates.

               

              MBS("CURL.SetOptionSSLVerifyPeer"; $curl ; 0 )

              MBS("CURL.SetOptionSSLVerifyHost"; $curl ; 0 )

               

              Well, I hope that helps bait your hook.

              • 4. Re: Posting JSON to RESTful API
                Mike Duncan

                The MBS plugin is great, there may also be other plugins (like the base elements plugin) that do curl functions. Depending on your needs, you may also be able to do this without plugins via javascript in a webviewer. You can specify the method, like PUT of DELETE but you may be limited more due to cross site scripting settings on the server you are talking to.

                 

                Mike

                • 5. Re: Posting JSON to RESTful API
                  jeffalmquist

                  Dan, did you ever get this figured out? I'm trying to do the same thing now (submit/POST FileMaker data as JSON to a RESTful API).

                  • 6. Re: Posting JSON to RESTful API
                    danjamins

                    Yes sir I did. I'll post an example in a few minutes, I'm eating lunch right now.

                     

                    I used both the Base Elements plugin and the MBS plugin. The Base Elements plugin is much easier and free to use, MBS was a little more difficult to setup and also cost money but had a guide on their site on how to do it so it made it easy to figure out. Base Elements I had to do some digging around on how to set it up to work the way I wanted.

                    • 7. Re: Posting JSON to RESTful API
                      danjamins

                      Ok jeffalmquist

                       

                      Here is what you do if you're using the Base Elements free plugin:

                       

                      1. Set your JSON to a variable. I used a custom function I made to encode JSON items and such but you can do whatever you want:

                           $JSON = (your JSON here.)

                       

                      2. Set the URL:

                           $URL = "your url here"

                       

                      3. Set your headers (you can do this multiple times before actually posting):

                           $Header = BE_HTTP_Set_Customer_Header ( "Content-Type" ; "application/json" )

                       

                      4. POST the data:

                           $Post = BE_HTTP_POST ( $URL ; $JSON )

                       

                      That's all you need to actually send the JSON. But if you want debugging, here is what you will need to do:

                       

                      5. Grab the debug info:

                           $DebugInfo = BE_Curl_Trace (you can also set this to a field instead of a variable)

                       

                      6. Grab the response code:

                           $Response = BE_HTTP_Response_Code (could use a variable or a field here as well)

                       

                      7. Grab the response headers:

                           $ResponseHeader = BE_HTTP_Response_Headers (can also be a field)

                       

                       

                      Hope that helps. I didn't put up the MBS version because it's a lot longer and you can view a guide on the MBS website.

                      1 of 1 people found this helpful
                      • 8. Re: Posting JSON to RESTful API
                        monkeybreadsoftware

                        Strange. CURL.SetOptionPostFields normally switches to POST automatically, so CURL.SetOptionPost and CURL.SetOptionCustomRequest are not needed.

                         

                        And if you don't specify a certificate file, the CURL.SetOptionSSLVerifyPeer and CURL.SetOptionSSLVerifyHost should be set to zero automatically.

                         

                        All with recent plugins as older versions may work different.

                         

                        • 9. Re: Posting JSON to RESTful API
                          jeffalmquist

                          Brilliant! Thanks, Dan, for that detailed guidance. BTW, I also found this article and demo file from Mike Duncan (Soliant Consulting), which uses Javascript/AJAX and a web viewer to GET, POST, PUT or DELETE (no plugins or custom functions!) Mike's demo file works beautifully and does everything I need. Thank you, Mike!  http://www.soliantconsulting.com/blog/2014/11/filemaker-and-javascript-ajax-post

                          3 of 3 people found this helpful
                          • 10. Re: Posting JSON to RESTful API
                            danjamins

                            Thank you, and also thank you for providing that link. That's excellent! We made it by with 40 clients using the plugin but having them turn on "enable solutions to allow plugins" was a bit of a process since nobody pays attention to tech emails lol.

                             

                            Anyway, it would have been great to implement it that way where no plugins are necessary. Glad I could help anyway!