This topic was automatically generated from Slack. You can find the original thread here.
I am trying to fetch a PDF report from an API and send it as an HTTP response in my Pipedream workflow. I attempted to use $.send.file, but encountered an error stating that $.send.file is not a function. Could you please confirm the correct method or function to use in Pipedream for sending a file (such as a PDF) as an HTTP response?
Here is a brief outline of my use case:
To send a PDF report (or any file) as an HTTP response in a Pipedream workflow, you’ll want to use the $.send.http() method to send the file back in response to an HTTP request. However, it’s important to note that $.send.file is not a valid method, which is consistent with the error you encountered. Instead, you should fetch the PDF file from the external API using an HTTP request, then use $.send.http() to send the file content as an HTTP response. Here’s a basic outline of how you can achieve this: 1. Fetch the PDF report from the external API using an HTTP request. You can use the axios library for this purpose. 2. Once you have the PDF content, use $.send.http() to send it as an HTTP response. Below is an example code snippet that demonstrates this process: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { // Fetch the PDF report from the external API const response = await axios($, { method: "GET", url: "URL_OF_THE_PDF_REPORT", // Replace with the actual URL responseType: "arraybuffer", // Important for binary content like PDF }); // Send the PDF as an HTTP response $.send.http({ body: response.data, headers: { "Content-Type": "application/pdf", "Content-Disposition": `attachment; filename="report.pdf"`, }, }); }, }); Please replace "URL_OF_THE_PDF_REPORT" with the actual URL from which you’re fetching the PDF report. This example uses responseType: "arraybuffer" in the axios request to handle the binary content of the PDF file correctly. The $.send.http() method then sends this binary content as the body of the HTTP response, setting the appropriate Content-Type and Content-Disposition headers to indicate that the response is a PDF file. This approach should allow you to fetch a PDF report from an external API and send it as an HTTP response in your Pipedream workflow.
How can I modify this script to run in a node.js instance in Pipedream?
import axios from "axios";
export default defineComponent({
async run({ steps, $ }) {
const apiKey = process.env.SE_Ranking;
// Log the API key (just for debugging; remove or comment out in production)
console.log("API Key:", apiKey);
if (!apiKey) {
throw new Error("API key is not set. Please ensure the environment variable SE_Ranking is configured correctly.");
}
// Retrieve the website URL from the provided location
const websiteUrl = steps.trigger.event.newRow[4];
// Log the website URL (just for debugging; remove or comment out in production)
console.log("Website URL:", websiteUrl);
if (!websiteUrl) {
throw new Error("websiteUrl is not defined in the event payload");
}
// Function to create an audit
async function createAudit(domain) {
try {
const response = await [axios.post](http://axios.post)("https://api4.seranking.com/audit/create", {
domain: domain,
// Include any other necessary parameters here
}, {
headers: {
Authorization: `Token ${apiKey}`,
'Content-Type': 'application/json' // Ensure content type is set
}
});
console.log("Audit creation response:", response.data);
if (response.data && response.data.id) {
return response.data.id;
} else {
throw new Error("Invalid response from SE Ranking API");
}
} catch (error) {
console.error("Error creating audit:", error.message);
console.error("Error details:", error.response ? error.response.data : 'No response data');
throw error;
}
}
// Function to get the PDF report
async function getAuditReport(auditId) {
try {
const response = await axios.get(`https://api4.seranking.com/audit/${auditId}/report`, {
responseType: 'arraybuffer',
headers: {
Authorization: `Token ${apiKey}`
}
});
if (response.data) {
return response.data;
} else {
throw new Error("Invalid response from SE Ranking API when fetching the report");
}
} catch (error) {
console.error("Error fetching audit report:", error.message);
console.error("Error details:", error.response ? error.response.data : 'No response data');
throw error;
}
}
// Function to add delay
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Main function logic
try {
const auditId = await createAudit(websiteUrl);
// If auditId is obtained, log it for debugging
console.log("Audit ID:", auditId);
// Wait for some time to ensure the audit is completed before fetching the report
await delay(60000); // Wait for 60 seconds
const auditReport = await getAuditReport(auditId);
// Export the PDF file
$.exportFile('audit_report.pdf', auditReport, 'application/pdf');
return {
auditId,
message: "Audit report generated and exported successfully."
};
} catch (error) {
console.error("Error in main function:", error.message);
throw error;
}
}
});
To modify the script to run in a Node.js instance in Pipedream, we need to adjust the environment variable access and ensure the use of the axios import from the @pipedream/platform package. Additionally, since you’re using a delay function, we’ll leverage Pipedream’s built-in $ object for that. Here’s the modified script: ``` import { axios } from “@pipedream/platform”; export default defineComponent({ async run({ steps, $ }) { // Accessing environment variables in Pipedream const apiKey = this.se_ranking.$auth.api_key; // Log the API key (just for debugging; remove or comment out in production) console.log(“API Key:”, apiKey); if (!apiKey) { throw new Error(“API key is not set. Please ensure the SE Ranking API key is configured correctly in the connected app.”); } // Retrieve the website URL from the provided location const websiteUrl = steps.trigger.event.newRow[4]; // Log the website URL (just for debugging; remove or comment out in production) console.log(“Website URL:”, websiteUrl); if (!websiteUrl) { throw new Error(“websiteUrl is not defined in the event payload”); } // Function to create an audit async function createAudit(domain) { try { const response = await axios($, { method: “post”, url: “https://api4.seranking.com/audit/create”, data: { domain: domain, // Include any other necessary parameters here }, headers: { Authorization: Token ${apiKey}, ‘Content-Type’: ‘application/json’ // Ensure content type is set } }); console.log(“Audit creation response:”, response.data); if (response.data && response.data.id) { return response.data.id; } else { throw new Error(“Invalid response from SE Ranking API”); } } catch (error) { console.error(“Error creating audit:”, error.message); console.error(“Error details:”, error.response ? error.response.data : ‘No response data’); throw error; } } // Function to get the PDF report async function getAuditReport(auditId) { try { const response = await axios($, { method: “get”, url: `
import { axios } from "@pipedream/platform";
export default defineComponent({
async run({ steps, $ }) {
// Accessing environment variables in Pipedream
const apiKey = this.se_ranking.$auth.api_key;
// Log the API key (just for debugging; remove or comment out in production)
console.log("API Key:", apiKey);
if (!apiKey) {
throw new Error("API key is not set. Please ensure the SE Ranking API key is configured correctly in the connected app.");
}
// Retrieve the website URL from the provided location
const websiteUrl = steps.trigger.event.newRow[4];
// Log the website URL (just for debugging; remove or comment out in production)
console.log("Website URL:", websiteUrl);
if (!websiteUrl) {
throw new Error("websiteUrl is not defined in the event payload");
}
// Function to create an audit
async function createAudit(domain) {
try {
const response = await axios($, {
method: "post",
url: "https://api4.seranking.com/audit/create",
data: {
domain: domain,
// Include any other necessary parameters here
},
headers: {
Authorization: `Token ${apiKey}`,
'Content-Type': 'application/json' // Ensure content type is set
}
});
console.log("Audit creation response:", response.data);
if (response.data && response.data.id) {
return response.data.id;
} else {
throw new Error("Invalid response from SE Ranking API");
}
} catch (error) {
console.error("Error creating audit:", error.message);
console.error("Error details:", error.response ? error.response.data : 'No response data');
throw error;
}
}
// Function to get the PDF report
async function getAuditReport(auditId) {
try {
const response = await axios($, {
method: "get",
url:
``` import { axios } from “@pipedream/platform”; export default defineComponent({ async run({ steps, $ }) { // Accessing environment variables in Pipedream const apiKey = this.se_ranking.$auth.api_key; // Log the API key (just for debugging; remove or comment out in production) console.log(“API Key:”, apiKey); if (!apiKey) { throw new Error(“API key is not set. Please ensure the SE Ranking API key is configured correctly in the connected app.”); } // Retrieve the website URL from the provided location const websiteUrl = steps.trigger.event.newRow[4]; // Log the website URL (just for debugging; remove or comment out in production) console.log(“Website URL:”, websiteUrl); if (!websiteUrl) { throw new Error(“websiteUrl is not defined in the event payload”); } // Function to create an audit async function createAudit(domain) { try { const response = await axios($, { method: “post”, url: “https://api4.seranking.com/audit/create”, data: { domain: domain, // Include any other necessary parameters here }, headers: { Authorization: Token ${apiKey}, ‘Content-Type’: ‘application/json’ // Ensure content type is set } }); console.log(“Audit creation response:”, response.data); if (response.data && response.data.id) { return response.data.id; } else { throw new Error(“Invalid response from SE Ranking API”); } } catch (error) { console.error(“Error creating audit:”, error.message); console.error(“Error details:”, error.response ? error.response.data : ‘No response data’); throw error; } } // Function to get the PDF report async function getAuditReport(auditId) { try { const response = await axios($, { method: “get”, url: https://api4.seranking.com/audit/${auditId}/download, headers: { Authorization: Token ${apiKey}, }, }); console.log(“Audit Report Download URL:”, response.data); if (response.data && response.data.url) { return response.data.url; } else { throw new Error("Invalid response from SE Ranking API when