Trying to fetch file contents with ServiceCallout, not working. Help?

Not applicable

Hi , I'm a beginner in Apigee Edge. I need your suggestions to fetch file contents using a ServiceCallout policy.

My requirement is that I need to download a csv file using a ServiceCallout and need to process its content.

My ServiceCallout policy is like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="downloadAPI">
  <DisplayName>downloadAPI</DisplayName>
  <Properties/>
  <Request clearPayload="true" variable="myRequest">
  <Set>
  <Headers>
  <Header name="Content-Type">application/json</Header>
  <Header name="Authorization">Bearer {shareFiles.auth_token}</Header>
  </Headers>
  <Verb>GET</Verb>
  </Set>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  </Request>
  <Response>calloutResponseDownload</Response>
  <HTTPTargetConnection>
  <Properties/>
  <URL>https://{hostName}/{downloadUrl}</URL>
  </HTTPTargetConnection>
</ServiceCallout> 

When I try this url in POSTMAN the file is getting downloaded. When I'm trying the same using a ServiceCallout, it is not working.

When I'm print the response (print(context.getVariable("calloutResponseDownload.content"));) it shows as empty.

0 5 526
5 REPLIES 5

If I were diagnosing this, I would enable Trace for the API Proxy in question.

You should be able to examine the inputs and outputs of the ServiceCallout.

Some questions for you to explore:

  1. Is it possible that the input variables that are used by the SC policy to set the URL, in your case, hostName and downloadUrl, are not set correctly, to the expected values? If either are incorrect or unset , then the URL used by the SC policy will be incorrect and obviously the download will fail. Reminder: CaSe mAtTeRs in variable names. If you set variable 'hostname' and refer to variable 'hostName' in the URL, you may get a null/blank value.
  2. Likewise re you sure the shareFiles.auth_token variable is set properly? If it has been set incorrectly, or not set at all, your service may return 401/403, and the response will not be what you expect. The endpoint may be returning a 401 with no content, which would explain why "calloutResponseDownload.content" is empty. You will also want to examine calloutResponseDownload.status.code .
  3. Is it possible that the endpoint denoted by https://{hostname} is reachable from your workstation running postman, but is not reachable from the Apigee Edge instance which is running on the public internet. If I set up an endpoint on my local machine, that is accessible from any node on my local network, and yet protected from access from any node running _outside_ my local network, then... a request from POSTMAN would succeed, but a request from ServiceCallout would fail.

7294-apigee1.png

The hostName,downloadUrl and auth_token are set correctly.Got the successful response (status 200) but when I'm print the response it shows as empty

print(context.getVariable("calloutResponseDownload.content"));

is it possible the downloaded content is not a string? Not a printable value? If it's a byte array, it won't print properly.

I'm not sure what I'm looking at with your modified screenshot. the JS getFileContent... I don't know what that is. Maybe that is the thing in which you reference calloutResponseDownload.content .

In the trace, You can highlight a particular step and see exactly what happens for that step. Have you done that?

The SC callout URL looks right to you, but did you actually verify that you got a valid response? I don't see that in anything you've shown me.

Same issue im getting. undefined when extracting context.getVariable("serviceCalloutResponse.content");

old question... .but it may be that the response is chunked or streamed and the full content is not available.

OR you may be referring to an incorrect variable.

It's impossible to say.

Just an additional comment:

"It's not working for me, the variable is undefined" is not enough information for anyone to be able to offer much help to you. Details in your question can help people provide educated guesses as to answers. But please don't ask new questions here, in the comment stream of a 18-month old question. Yes, I know it's similar, but it's also a totally different user, different policy, different problem. Open a new thread please.