Hi everyone,
I’m building an n8n workflow to pull product information from the Product Hunt API, specifically the product name, tagline, description, and website URL. I’m then writing this data to a Google Sheet.
I’m having trouble resolving the final destination URL from the Product Hunt website URLs. I need to follow the redirects to get the actual website address, but I keep getting null
values for the final URL.
Here’s what my workflow looks like:
- Daily Trigger: Triggers the workflow daily.
- Set Date: Sets the date for the Product Hunt API query.
- HTTP Request: Fetches the product data from the Product Hunt API.
- Code (Format and Resolve Redirects): (See code below) - This is where I’m having the issue.
- Google Sheets: Writes the data to a Google Sheet.
I’m using a Code node with JavaScript to extract the data and attempt to resolve the redirects. I’m using n8n’s $http.request
to follow the redirects, but for some reason the finalUrl still show up as null
.
Here’s the code in my Code node:
javascript
// Code node to extract product data and resolve redirects
async function resolveRedirect(url) {
try {
const response = await $http.request({
url: url,
method: 'GET',
followRedirect: true,
});
return response.request.options.uri; // The final URL after redirects
} catch (error) {
console.error("Error resolving redirect:", error);
return null;
}
}
const products = items[0]?.json?.data?.posts?.edges || [];
const results = [];
for (const edge of products) {
const node = edge.node;
let finalUrl = null;
if (node?.website) {
finalUrl = await resolveRedirect(node.website);
}
results.push({
json: {
name: node?.name || null,
tagline: node?.tagline || null,
description: node?.description || null,
website: node?.website || null, // Original Product Hunt URL
finalUrl: finalUrl, // Final URL after redirect
},
});
}
return results;
I’ve tried various approaches, but I’m still stuck. Any help or suggestions would be greatly appreciated!
Thanks in advance!
Check screenshorts of the issues
Citations:
- https://pplx-res.cloudinary.com/image/upload/v1744789540/user_uploads/AZvIkpIPotbCXTw/image.jpg
- https://pplx-res.cloudinary.com/image/upload/v1744789753/user_uploads/cCNtIXpCoYPztdw/image.jpg
- https://pplx-res.cloudinary.com/image/upload/v1744790675/user_uploads/SeLuFikpHpLgIdD/image.jpg
- https://pplx-res.cloudinary.com/image/upload/v1744790678/user_uploads/HJtyyiUBPRoBWMI/image.jpg