Question

Issues passing JSON to a webhook via AJAX HTTP POST request

  • 19 January 2023
  • 5 replies
  • 42 views

Hello again everyone,

 

I have a question for anybody who's worked with passing JSON data in the body of a POST request for a webhook.

 

I am working with a developer on a script to call FME server from an outside application and pass the needed JSON data in the body. She is using an AJAX script that looks somewhat like this:

 

$.ajax({

        url: <my server url with webhook token>,

        method: 'POST',

        data: {"A":"1", 

        "B":"2",

        "C":"3",   

        },

        dataType: "json",

        success: function (response) {

                             alert(JSON.stringify(response));      

               },

               error: function(data, errorThrown){

                             alert('request failed :'+errorThrown);

                             //document.getElementById("response").innerHTML="Failed"+errorThrown;

             }

});

 

Here's the thing: I have a separate HTTPCaller on a 'tokentester' workbench (as a substitute for something like POSTMAN) and it works fine through there. In this case, I have all the headers and the JSON is being passed through the 'Specify Upload Body' option.

 

When I look through the logs of my requests (ie. done through HTTPCaller) I see that the URL submitted to the server under 'FME_SERVER_REQUEST_URI' is <my server url with webhook token> (so, exactly what I would expect). The server is able to use the JSON as passed through HTTPCaller and the request is a success.

 

However, when I see the logs from her requests, the 'FME_SERVER_REQUEST_URI' value is something like: "<my server url with webhook token>&A=1&B=2&C=3". In other words, FME Server is receiving her AJAX request as the URL PLUS the JSON's individual elements in the same string. The server then throws an error saying it can't find the original JSON file uploaded with the workbench.

 

In other words, FME Server is receiving the request with all the data embedded in the URL in a way it doesn't understand (or expect) and so thinks the JSON body is missing, thus it defaults to the 'original' file uploaded with the workbench.

 

I'm not very familiar with AJAX and its methods to send requests. Does anyone have an idea as to what might be going on? Is there something that can be added to the code so FME Server gets the right request, or perhaps something I need to tweak on my own end in the workbench?

 

Thanks for reading! Any insight is most welcome!

 

 

 

 

 

 

 

 

 

 

 

 

 


5 replies

Userlevel 4

My first impression is that it looks fairly correct, but it's difficult to tell what the problem is without the precise error messages mentioned. Did you verify that the parameters transmitted on the URL correspond to published parameters in the called workspace?

If you haven't read it already, this is a great starting point to understand how this works: https://community.safe.com/s/article/Submitting-a-Job-through-FME-Server-Webhook-URLs

My first impression is that it looks fairly correct, but it's difficult to tell what the problem is without the precise error messages mentioned. Did you verify that the parameters transmitted on the URL correspond to published parameters in the called workspace?

If you haven't read it already, this is a great starting point to understand how this works: https://community.safe.com/s/article/Submitting-a-Job-through-FME-Server-Webhook-URLs

Hello David, thanks for the reply!

 

Yes, I have gone through that resource and it's the one I've been trying to adapt for my purposes.

 

So the thing is the only published parameter in my workspace is the JSON file input. The actual JSON elements are not meant to be passed as parameters.

 

We did figure out that the reason, perhaps, FME Server was receiving things this way was because the developer was sending the request with the token appended in the query (what the webhook calls 'Authorization with Query String' - "http://urladdress&token=<token>"), whereas when it's sent with the headers as separate ('Authorization with Header' - http:urladdress; headers: {authorization: fme token...etc.}) then the call is received properly by FME Server and it recognizes that there's a JSON body to process.

 

The problem we're running into now that is that FME claims there's a syntax error in the JSON being passed even though everything looks fine on the developer's end. I've put a request to the admin team in my office, asking them if it's possible to access what was sent (the logs say it's stored in the resources/system/temp folders) so that we can see where the error is coming from and what FME is seeing.

 

Quick follow-up, and I'm hoping this is a much more straightforward question.

 

I found this forum thread:

 

https://community.safe.com/s/question/0D54Q00008IZBxWSAX/looking-for-a-howto-for-sending-data-to-fme-server-via-rest-service

 

If I am reading it correctly, the author is sending the JSON body to the server in a stringified format and says it works.

 

Does FME Server require that the JSON body be stringified in the HTTP request? The developer I'm working with says that stringifying is only used for responses, but that forum thread above (and the fact that FME complains about an otherwise valid JSON) makes me wonder. I unfortunately don't have the tools nor role within my company to test this myself, which is why I'm trying to gather leads so I can bring them to the developer.

Badge +7

Hi @rinfante_coc​ , if I understand correctly from your response to David, and your workspace is expecting the whole JSON object as one parameter (not individual parameters for A, B, C), then you can try appending it as a URL-encoded escaped string to the webhook URL. For example:

Escaped string 
{\"A\":\"1\",\"B\":\"2\",\"C\":\"3\"}
 
URL-encoded passed as parameter in URL:
myParameter=%7B%5C%22A%5C%22%3A%5C%221%5C%22%2C%5C%22B%5C%22%3A%5C%222%5C%22%2C%5C%22C%5C%22%3A%5C%223%5C%22%7D

But I think it would be more straightforward to use the FME Server REST API.

Take a look at Transformations.

We have an asynchronous API: POST /transformations/submit/< repository >/< workspace >

And a synchronous API: POST /transformations/submit/< repository >/< workspace >

If passing JSON as the parameter, it needs to be an escaped string, like my first example (doesn't need to be URL-encoded).

 

Another option could be to create an FME Server Automation with a Webhook Trigger. Then you can pass your JSON in the request body like you currently are and the Automation will receive the raw JSON in the Webhook Message Content key that you can pass into your workspace parameter within the automation.

 

 

Hi @rinfante_coc​ , if I understand correctly from your response to David, and your workspace is expecting the whole JSON object as one parameter (not individual parameters for A, B, C), then you can try appending it as a URL-encoded escaped string to the webhook URL. For example:

Escaped string 
{\"A\":\"1\",\"B\":\"2\",\"C\":\"3\"}
 
URL-encoded passed as parameter in URL:
myParameter=%7B%5C%22A%5C%22%3A%5C%221%5C%22%2C%5C%22B%5C%22%3A%5C%222%5C%22%2C%5C%22C%5C%22%3A%5C%223%5C%22%7D

But I think it would be more straightforward to use the FME Server REST API.

Take a look at Transformations.

We have an asynchronous API: POST /transformations/submit/< repository >/< workspace >

And a synchronous API: POST /transformations/submit/< repository >/< workspace >

If passing JSON as the parameter, it needs to be an escaped string, like my first example (doesn't need to be URL-encoded).

 

Another option could be to create an FME Server Automation with a Webhook Trigger. Then you can pass your JSON in the request body like you currently are and the Automation will receive the raw JSON in the Webhook Message Content key that you can pass into your workspace parameter within the automation.

 

 

Hello Matt!

 

Thanks for your reply, and apologies for how long it took me to get back to you.

 

Yes, so the idea was to write a script within this outside application that would trigger the workbench via a Webhook that I had set up and also send in the JSON content needed for the job. For whatever reason, we were having issues getting FME Server to parse or understand any of the parameters when they were appended in the URL. We instead ended up calling the following AJAX script from that outside application:

 

url: <Webhook URL>,

method: 'POST',

headers: {

                "Authorization":<webhook token>,

                "Content-Type":"application/json",

                "Accept":"application/json"

},

              data: json.stringify({<JSON Data>}),

 

In other words, yes: we did have to 'stringify' the JSON before passing it to the server. I guess it's the same as the escaped string you mention

 

So, all in all, we got things to work!

 

 

 

 

 

 

 

Reply