Rewrite a GET request with URl rewrite and method transform


#1

I am trying to make a GET request transform to a POST request with a URL rewrite.

Request:
GET /foo

Desired result:
POST /bar -d ‘{json stuff}’

I’ve tried a number of things with the URL rewrite in the UI, but it doesn’t seem to work. Is there a way to do this in the UI? If not, is there a way to do it with Middleware? If so, how?

Thanks,
Mike

Related Issue


#2

In addition to URL rewrite you should additionally use “Method transform” plugin.

Additionally in 2.8/1.8 you can use looping like this:


#3

Thank you Leon, that seems to work for the translating the request.

Another question: The body transform doesn’t appear to be working now. I have a template where I’m attempting to insert a path parameter, but it isn’t including the template on the request.




Response from httpbin

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "application/json",
    "Accept-Encoding": "application/json",
    "Content-Length": "0",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0",
    "X-Authentication-Token": "<removed>"
  },
  "json": null,
  "origin": "10.60.2.49, 38.135.95.33, 10.60.2.49",
  "url": "https://httpbin.org/post"
}

#4

Also, “enable context variables” is set to true


#5

More research:
It has to do with the {{._tyk_context.path_parts[0]}} variable. If I hard code that, everything works


#6

Try using the following expression:

{{index ._tyk_context.path_parts 0}}

In general it use Go templates syntax https://golang.org/pkg/text/template/#hdr-Actions


#7

Yes, that works. I thought I saw somewhere in your docs to use path_parts[0], but it must have been something else (obviously a different language).

Thanks!