Requesting field config results in INVALID_TARGET_URL error

Hello,
I’m running into a problem trying to get configuration of a custom field. Requests fail on this bit of code:

const fieldId = "customfield_10051";
const config = await api.asApp().requestJira(
  route`/rest/api/3/app/field/${fieldId}/context/configuration`,
  {
    headers: {
      Accept: "application/json",
    },
  },
);

And this is the error I get in the tunnel console:

ERROR   12:23:19.165  e9019845-5d46-4550-bd1a-f9857bcc50e0  Error in handler getFieldConfig PROXY_ERR: Forge platform failed to process runtime HTTP request - 400 - INVALID_TARGET_URL
    at handleProxyResponseErrors (webpack:///node_modules/.pnpm/@forge+api@3.8.0_@swc+core@1.4.1/node_modules/@forge/api/out/api/fetch.js:84:1)
    at <anonymous> (webpack:///node_modules/.pnpm/@forge+api@3.8.0_@swc+core@1.4.1/node_modules/@forge/api/out/api/fetch.js:29:1)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 400,
  errorCode: 'INVALID_TARGET_URL'
}

It’s notable that just entering the URL from the request into a browser returns the configuration just fine.
The error seems similar to the one discussed here: Proxy error in development and staging when calling Confluence attachments API with requestConfluence
Could I get some assitance, please?

Hi @ViktorWolf1, are you able to provide us with your appId?

Hi, our appId is: ari:cloud:ecosystem::app/c6e2b50d-017f-47fa-818d-52cfb113da35

Hi @ViktorWolf1, thank you for providing your appId. I wasn’t able to reproduce the error using the code snippet you provided.
I had a look at the logs for your app, this particular error case that you are running into occurs when the base URL of the request is overridden to something that is not valid, for example: if the route passed into the request was a full URL like https://exampleurl.com or if it is something like ../upOneLevel. Are you able to confirm that something like this is not happening for your app?

2 Likes

OK, I have figured it out. When I posted the code here, I changed the formatting, which inadvertently ended up masking the cause of the issue - the actual code had the string template on a new line, so it had the newline character (\n) at its beginning.

I have no idea how I missed that in the first place, and somehow it didn’t occur to me to just log the resulting route before reading your post, so thanks for pointing me in the right direction. I’m sorry for bothering you with such a silly mistake.

1 Like

No worries @ViktorWolf1, I am glad to hear that it is now working for you!

Hi @BoZhang, I am running into the same problem but it does not seem like I have the same cause. Could you or anyone available please help me take a look?

Here is the code

const url = assumeTrustedRoute(
    `${base}/pipelines?page=1&pagelen=20&sort=-created_on&target.branch=master&status=PASSED&status=SUCCESSFUL&trigger_type=PUSH`,
  );

  const result = await (await App.asApp().requestBitbucket(url)).json();
  return result?.values ?? [];

Here is the resolved url by @forge/api that is given to fetch: https://bitbucket/2.0/repositories/<mysite>/<myrepo>/pipelines?page=1&pagelen=20&sort=-created_on&target.branch=master&status=PASSED&status=SUCCESSFUL&trigger_type=PUSH

I presume that https://bitbucket will be later resolved into an URL by Forge Proxy :thinking:
I can provide you with the appId if needed

Hi @BrianPham, my first guess is that the path parameters might not be correct for the API request that you are attempting to make. Where are you getting the params for mysite and myrepo in /2.0/repositories/<mysite>/<myrepo>?

mysite should be your Bitbucket workspaceId and myrepo should be your repositoryId. You should be extracting them like this:

resolver.define("fetchRepository", async ({ context }) => {
  const workspaceId = context.workspaceId;
  const repositoryId = context.extension.repository.uuid;

  // Base should be constructed using workspaceId and repoId from the context passed to your resolver
  const base = `/2.0/repositories/${workspaceId}/${repositoryId}`;
  //.... other application specific logic ...
});

Let me know if this isn’t the issue. If not, can you please provide me with your appId and create another thread to make sure this thread doesn’t deviate. Thanks!

1 Like