Encountering HTTP 400 when calling mintNFT despite successful image uploads and valid keys. Below are new code examples:
async function pushToIPFS(fileObj) {
if (!fileObj) return;
const formPayload = new FormData();
formPayload.append('document', fileObj);
try {
const reply = await axios.post('https://api.pinata.cloud/pinning/filePin', formPayload, {
headers: { 'api-key': 'yourKey', 'secret-key': 'yourSecret', 'Content-Type': 'multipart/form-data', 'Auth': `Bearer yourJWT` }
});
return `https://gateway.pinata.cloud/ipfs/${reply.data.hash}`;
} catch (err) {
console.error('Upload error:', err);
}
}
async function mintNFT(title, cost, mediaUrl, details, navigate) {
if (!title || !cost || !mediaUrl || !details) return console.log('Missing data');
const payload = JSON.stringify({ title, details, mediaUrl });
try {
const res = await axios.post('https://api.pinata.cloud/pinning/filePin', payload, {
headers: { 'api-key': 'yourKey', 'secret-key': 'yourSecret', 'Content-Type': 'application/json', 'Auth': `Bearer yourJWT` }
});
const ipfsLink = `https://gateway.pinata.cloud/ipfs/${res.data.hash}`;
await listForSale(ipfsLink, cost);
navigate('/results');
} catch (error) {
console.error('Mint error:', error);
}
}
async function listForSale(link, price, isResell = false, tokenId = null) {
try {
const convertedPrice = ethers.utils.parseUnits(price, 'ether');
const contractInstance = await initiateContract();
const fee = await contractInstance.getFee();
const tx = isResell
? await contractInstance.resellToken(link, convertedPrice, { value: fee.toString() })
: await contractInstance.createToken(link, convertedPrice, { value: fee.toString() });
await tx.wait();
} catch (e) {
console.error('Sale error:', e);
}
}
I encountered a similar issue while testing my own NFT integration with Pinata. The 400 error I observed was eventually traced back to a mismatch between the endpoint and the payload type being used. In my case, switching from a file upload endpoint to one designed for JSON payloads resolved the issue. I also found that making sure the headers exactly match what the API documentation specifies was critical. Reviewing the documentation and ensuring that the endpoint is intended for the operation you are attempting can solve similar problems.
hey ninapaint, i was playin around with a similar setup last week. it struck me that maybe the error might be coming from how the server interprets your json payload and headers for mintNFT. i once had a head-scratcher where a tiny misnaming of header fields caused that 400 error. have you tried swapping out ‘Auth’ with something like ‘authorization’ or maybe even removing it if not needed? also, sometimes the minting endpoint expects additional meta fields that might not be there - did you check if metadata structure has to be nested or include a version number?
i’m super curious what kind of tweaks others have tried. maybe comparing the request payload in a tool like postman could help narrow it down. anyone got ideas on how to simulate the exact api call so we can spot differences? let’s figure this out together!
hey ninapaint, i was just looking at your code and it kinda caught my attention that you’re using JSON payload for the mintNFT call while the endpoint is still the one for filePin. i wonder if the api might be expecting a formdata payload like in the pushToIPFS function? i had a similar hiccup a while back when the headers weren’t matching the expected content. did you try checking out the pinata docs to see if there’s a specific endpoint for metadata minting instead of just file uploads?
also, have you noticed if the error message gives any more details? sometimes the 400 error hints at missing or misformatted fields. maybe tinkering with the header names or even the method of sending data (like using formdata for metadata) might reveal further insights.
what do you think, anyone else run into something similar? would love to hear more about your debugging steps if you dug deep into this!
hey ninapaint, i had a similar issue. perhaps your payload headers or endpoint selection is off. try double-checking the docs for the proper endpoint for json vs fileupload. sometimes a small typo can lead to a 400 error.