Transform GET to POST, adding a body

Hello, I’m trying to transform a GET request (without a body) into a POST request with a body. I’m setting up the request using the endpoint designer.

I’ve added a GET request with the path “graphql” (I’ll be adding a URL rewrite later)
To this endpoint, I’ve added a method transform to change the method into a POST request.
I’ve also added a body transform:
{"query":"{\n currentMember {\n lastName\n }\n}","variables":{}}

However, the body is not added unless the original GET request includes a body. I do not want the original get request to include a body. So, how can I transform a GET request (without a body) into a POST request with a body?

Here is the raw transform definition:

 {
"transform": [
              {
                "disabled": false,
                "template_data": {
                  "input_type": "json",
                  "template_mode": "blob",
                  "enable_session": false,
                  "template_source": "eyJxdWVyeSI6IntcbiAgY3VycmVudE1lbWJlciB7XG4gICAgbGFzdE5hbWVcbiAgfVxufSIsInZhcmlhYmxlcyI6e319",
                  "input": "",
                  "output": ""
                },
                "path": "graphql",
                "method": "GET"
              }
            ]

I’m using Tyk 4.2.3

Hey @Toni_Kaplan ,
welcome to the community!

Let me make sure I understand what you’re trying to achieve first. My understanding is you have a GraphQL upstream and instead of opening it up to everyone and allowing them to send all kinds of queries, you would rather persist a specific query and expose it as a REST endpoint. Is that correct?

If so, then I would advise switching to Tyk Gateway 4.3.0 and using a plugin we made available that allows you to do it really quickly. Here’s the docs on that feature: Persisting GraphQL queries
Basically you’d take a GraphQL upstream URL and create a HTTP proxy API in Tyk. Then in the raw definition of the API you add the following config in extended_paths section:

          "persist_graphql": [
            {
              "method": "GET",
              "path": "/getCurrentMember",
              "operation": "query:{\n currentMember {\n lastName\n }\n}",
              "variables": {
              }
            }
          ]

It’s also possible to do with earlier versions of Tyk but much more complex - you would need to use 4 different plugins together:

  • Body transform
  • Modify headers
  • URL rewrite
  • Method transform

Here’s an example of how those would need to be configured:

          "extended_paths": {
            "transform": [
              {
                "disabled": false,
                "template_data": {
                  "input_type": "json",
                  "template_mode": "blob",
                  "enable_session": false,
                  "template_source": "eyJxdWVyeSI6InsgY29uZmVyZW5jZXMgeyAgbmFtZSAgeWVhciBsb2NhdGlvbnMgeyBjb3VudHJ5IHsgY29kZSBuYW1lfX19fSJ9",
                  "input": "",
                  "output": ""
                },
                "path": "/conferences/",
                "method": "GET"
              }
            ],
            "transform_response": [],
            "transform_headers": [
              {
                "delete_headers": [],
                "add_headers": {
                  "Content-Type": "application/json"
                },
                "path": "/conferences/",
                "method": "GET",
                "act_on": false
              }
            ],
            "transform_response_headers": [],
            "url_rewrites": [
              {
                "path": "/conferences/",
                "method": "GET",
                "match_pattern": "/conferences/",
                "rewrite_to": "https://api.react-finland.fi/graphql",
                "triggers": []
              }
            ],
            "method_transforms": [
              {
                "disabled": false,
                "path": "/conferences/",
                "method": "GET",
                "to_method": "POST"
              }
            ]
          }

Hope it helps!

2 Likes

Thanks, I didn’t have the “Modify headers” plugin setup, that is what was causing the body-transform not to run when the body was empty. I added that and it is now working.

I am also separately exploring the persist_graphql approach, but still wanted to understand why the body transform approach was failing. Thanks for explaining.

1 Like