Unable to rewrite url with path variable

I want to redirect below request

/student/{id}
to
xxx/registry/api/v1/Student/{id}

here is my json

{
“name”: “Student”,
“slug”: “test-api”,
“api_id”: “Student”,
“org_id”: “1”,
“use_keyless”:true,
“auth_configs”: {
“authToken”: {
“auth_header_name”: “Authorization”
}
},
“definition”: {
“location”: “header”,
“key”: “x-api-version”,
“response_processors”: [
{
“name”: “response_body_transform”,
“options”: {}
}
]
},
“version_data”: {
“not_versioned”: true,
“versions”: {
“Default”: {
“name”: “Default”,
“use_extended_paths”: true,
“extended_paths”: {
“ignored”: [],
“white_list”: [],
“black_list”: [],
“cache”: [“get”],
“transform”: [],
“transform_response”: [
{
“path”: “Student/{id}”,
“method”: “GET”,
“template_data”: {
“template_mode”: “file”,
“template_source”: “ewogICJpZCI6ICJ7ey52ZXJ9fSIKfQ==”,
“input_type”: “json”
}
}
],
“url_rewrites”: [{
“path”: “/student/{id}”,
“method”: “GET”,
“match_pattern”: “/(\w+)/(\w+)”,
“rewrite_to”: “xxxx/registry/api/v1/Student/$2”,
“triggers”: []

        }],
        "transform_headers": []
      }
  }
 }  
},
"proxy": {
  "listen_path": "/student/{id}",
  "target_url": "xxxx/registry/api/v1/Student/{id}",
  "strip_listen_path": true
},


"active": true,
"response_processors": [
  {
    "name": "response_body_transform",
    "options": {}
  }
] 

}

After hitting the url the id being passed to target url is incorrect. What is it that i am doing wrong here?

Hi @Saloni_Parekh, welcome to the community.

It seems your goal may already be possible in your current API definition. The proxy setting looks properly configured to forward the request to your backend. However, you can still achieve your goal in the following ways

Without URL Rewrite

"proxy": {
  "listen_path": "/student/{id}",
  "target_url": "xxxx/registry/api/v1",
  "strip_listen_path": false
}

With URL Rewrite

"url_rewrites": [
	{
		"path": "/",
		"method": "GET",
		"match_pattern": "/(\\w+)/(\\w+)",
		"rewrite_to": "xxxx/registry/api/v1/Student/$2",
		"triggers": []
	}
]
...
...
...
proxy": {
  "listen_path": "/Student/{id}",
  "target_url": "xxxx/registry/api/v1",
  "strip_listen_path": false
}

The path in URL rewrite or transform response is the sub-path, but you can always tell it to target root “/”

Hopes this helps.

This worked for me. Thank you. The other issue was that my id was in uid format so had to use different pattern for it

That’s great to hear. Glad I could assist.

1 Like