I have created an API proxy where I am setting the target endpoint dynamically using the JS Policy. The URL does not contain any path (i.e https://example.com). I can think of hacks to resolve this, but I want to know why am I getting this error. What if the target service has no path in the URL?
Here is a screenshot for your reference:
Solved! Go to Solution.
Apigee behaves this way because when you don't specify a path in the TargetConnection, Apigee automatically adds a default path (/).
This is handled in the getTargetPath() method:
@Override
@XmlTransient
public String getTargetPath() {
return this.path == null ? HttpProxyConnection.PATH_SEPARATOR : this.path;
}
However, when you set the target.url directly, this automatic behaviour doesn't occur. If the path is missing, Apigee throws an error due to the path validation logic:
private boolean validatePath(HTTPRequest request) {
if (request != null && request.requestURI != null && !HTTPMethod.CONNECT.equals(request.method)) {
if (request.requestURI.getURI().isEmpty()) {
this.requestListener.onException(this, false, new BadRequestException(ErrorCodes.EMPTY_PATH));
return true;
} else if (request.requestURI.getURI().startsWith("?")) {
this.requestListener.onException(this, false, new BadRequestException(ErrorCodes.BAD_PATH));
return true;
} else {
return false;
}
}
return false;
}
Hello,
Are you sure the issue is the fact that there is no trailing character post base path name? The reason I ask is I can successfully call into https://example.com as a target proxy endpoint without any subsequent suffix in the domain. Reference can be seen below:
Are you sure the JS policy is properly mapping the response to a domain name, and the result is not coming out as null (debug can help with the above)? Is there a specific reason why you are using JS, conditionalizing aspects of your flow/policy files would work here as well without the need for custom scripting
To answer your first question, yes. There is no suffix involved. Secondly, I am using JS policy to dynamically set the target endpoint which is fetched from the request payload.
I have attached the JS policy in the request preflow of the target endpoint. I have replicated the scenario with a mock target and attached the trace of it.
Just add a slash / at the end of your URL, https://example.com/
That's what I have been doing for now. But I want to know why Apigee is behaving this way.
Apigee behaves this way because when you don't specify a path in the TargetConnection, Apigee automatically adds a default path (/).
This is handled in the getTargetPath() method:
@Override
@XmlTransient
public String getTargetPath() {
return this.path == null ? HttpProxyConnection.PATH_SEPARATOR : this.path;
}
However, when you set the target.url directly, this automatic behaviour doesn't occur. If the path is missing, Apigee throws an error due to the path validation logic:
private boolean validatePath(HTTPRequest request) {
if (request != null && request.requestURI != null && !HTTPMethod.CONNECT.equals(request.method)) {
if (request.requestURI.getURI().isEmpty()) {
this.requestListener.onException(this, false, new BadRequestException(ErrorCodes.EMPTY_PATH));
return true;
} else if (request.requestURI.getURI().startsWith("?")) {
this.requestListener.onException(this, false, new BadRequestException(ErrorCodes.BAD_PATH));
return true;
} else {
return false;
}
}
return false;
}