How does the Cloud Code REST API handle post Form request data

Using the Cloud Code REST API

How to handle request data for post Form

I want to upload PNG images and process them in Cloud Code to generate IPFS

it should be possible, you can try with a small image and use logger.info(JSON.stringify(request)) to see how to access that data

Tried to report an error:
"error":"Unexpected token - in JSON at position 0"
my code:

Moralis.Cloud.define("fileUploadBuyLand", async function (request) {
  const logger = Moralis.Cloud.getLogger();
  logger.info(JSON.stringify(request))
  const fileName = request.params.fileName;
  const pngFile = request.params.pngFile;
  logger.info("fileName:" + fileName);

  const png_nftFile = new Moralis.File(fileName, pngFile);
  await png_nftFile.saveIPFS();

  const nftPath = png_nftFile.ipfs();
  console.log("png nftPath:" + nftPath);
  return "nftPath:"+nftPath;
})

did it make it to this line or it gives that error in a different place?

did you update your server to latest version?

I deleted some of the code,

Moralis.Cloud.define("fileUploadBuyLand", async function (request) {
  const logger = Moralis.Cloud.getLogger();
  logger.info("test info");
  logger.info(JSON.stringify(request))
  const fileName = request.params.fileName;
  logger.info("fileName:" + fileName);
})

This line of code is not executed:
logger.info("test info");
An error:
"error":"Unexpected token - in JSON at position 0"

Is it because the submission is a PNG file

Parsing JSON directly is an error

The current server version:0.0.308

Found that the API Doc has such an interface

But don’t know how to upload files?
https://deep-index.moralis.io/api-docs/#/storage/uploadFolder

The file has been successfully uploaded by referring to this tutorial:


java code:

            File pngFile = FileUtil.file(ResourceUtil.getResource("list-img.png"));
            String moralis_api_url = "https://deep-index.moralis.io/api/v2/ipfs/uploadFolder";
            JSONArray array = JSONUtil.createArray();
            JSONObject obj = JSONUtil.createObj();
            obj.putOpt("path", "RCT_PNG/" + pngFile.getName());
            obj.putOpt("content", pngToBase64(pngFile));
            array.add(obj);
            HttpResponse response = HttpRequest.post(moralis_api_url)
                    .header("X-API-KEY","you api key")
                    .header("Content-Type", "application/json")
                    .header("accept", "application/json")
                    .body(array.toString())
                    .execute();
            log.info("response:{}", response.body());

...
private static String pngToBase64(File file) {
        try {
            byte[] fileContent = Files.readAllBytes(file.toPath());
            String encodeToString = Base64.getEncoder().encodeToString(fileContent);
            log.info("pngFile Base64:{}", encodeToString);
            return encodeToString;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return null;
    }

I think that you should convert the file contents to base64 before uploading it, anyway, it looks like you made it work

“error”:“request entity too large”

you should upload only json files with this uploadFolder endpoint, it doesn’t work now with big file sizes