0

It seems to me that this is an extremely simple problem and task I'm trying to solve:

I have an HTML page I've made, extremely simple, the user types in a code they'll be given and presses the 'sign-in' button. This button triggers two API calls in sequence:

  1. a POST call to a FileMaker database on the same server, retrieving an access token and returning it.

  2. a GET call that executes a FileMaker script in that same database, retrieving a URL for the user based on the code they input.

Then, we do a redirect on the web-page taking them to their application... or at least that is what is supposed to happen. This error has been plaguing me for days and I'm sure at this point I must be missing something so fundamental and simple I'd never think of it. Below is all the code, with some obvious dummy info placed in for actual values:

    <script>

        function onSubmit() {

            token = document.getElementById('r0c0m2i1').value;

            // API Call to start session

                    async function GetToken() {

               let auth = {};

            const response = await
                  fetch('https://HostName/fmi/data/vLatest/databases/DatabaseName/sessions', { 
                method: 'POST',
                body: auth,
                headers: { 
                'Content-Type': 'application/json; charset=utf-8', 
                'Authorization': 'Basic Base64EncodedCredentials',
                     }, 
                               } 
                             );

                        let Result = await response;
                
            // console.log(data);

            return Result

                    }

                      SessionResult = GetToken();

            // API Call to Get call script and get data

            URL = "";

            // fetch('https://HostName/fmi/data/vLatest/databases/ccNP_Config/layouts/LayoutName/ScriptName?script.param=' + token)
            // .then(URL => response.json())
            // .then(json => data.innerHTML = JSON.stringify(json))
            // .catch(err => console.log('GET Request Failed', err));

            // alert(json);


            // Go to returned URL

            // window.location.replace(URL);
        }

    </script>

For what its worth, I'm not too concerned about the 'GET' call because I haven't even been able to really test that part with the 1st part not working yet.

https://help.claris.com/en/data-api-guide/content/log-in-database-session.html - The documentation from FileMaker for their Data API.

I've made many API calls and scripts within FileMaker using cURL protocols and way of doing things, so this is my first foray into Javascript API calls from a browser environment.

Basically, I'm getting a 200 status (indicating a successful call), but FileMaker returns a 1630 which apparently means the URL is incorrect in some fashion, with only the message below to guide my debugging:

message":"Unexpected token o in JSON at position 1","code":"1630"

According to everyone on the internet, this error indicates that there's some issue with my parsing or with the object in question (probably the body parameter in the POST call). I've tried making it a string, empty, an array; everything I can think of. It will not return the parameter successfully that the documentation indicates it should.

I have already done this API call in Postman successfully with all of my variations as well, but I know Postman is much easier to get a successful call for lots of reasons.

Please help guide my debugging and save me from this morass I've found myself stuck in.

CCI_Dev
  • 1
  • 2
  • `{}` when stringified becomes `[object Object]`. You need to use `body: JSON.stringify(auth)`? – Phil Dec 07 '22 at 00:40

0 Answers0