Is it possible to push media data with rest api?

default discord avatar
artmelkon
last month
14

my objective is to push an image data to media collection via rest api, using json, but I get an error message "No files were uploaded", and I'm using mongodb-community version



I'm using Postman to upload an image and post the data but it returns an error with "No files were uploaded

  • default discord avatar
    muperdev
    last month

    you are not uploading it



    problem is in the upload part

  • default discord avatar
    artmelkon
    last month

    Yes, you're right, the endpoint was incorrect. However, even though doing it right it still doesn't work for me. The endpoint is right, the content type is multipart/form-data, and the image is selected as a file. I don't know why there is http error 404



    I'm still having the same issue. Tried with Postman and Insomnia, and the 404 error persists. Unable to figure it out. Even, I have installed payload on two different machines but the result is the same.



    Screenshot_2023-08-04_at_8.28.41_AM.png
    Screenshot_2023-08-04_at_5.30.45_PM.png
  • discord user avatar
    dribbens
    Payload Team
    last month

    Instead of filename, change the key to file.



    From the docs:


    Send it all the data that your Collection requires, as well as a file key containing the file that you'd like to upload.
  • default discord avatar
    artmelkon
    last month

    that is exactly what I did. In both screenshots, the file is selected, but it still fails to upload to the media collection. It fails no matter what I do.





    Screenshot_2023-08-04_at_7.08.51_PM.png
    Screenshot_2023-08-04_at_7.10.11_PM.png
  • discord user avatar
    dribbens
    Payload Team
    last month

    The first screenshot you have it very close, but there is an extra "n" on the end. It should just be

    file

    , not

    filen

    .

  • default discord avatar
    muperdev
    last month

    I was thinking maybe he doesn't allow create on permission section

  • default discord avatar
    artmelkon
    last month

    Thank you for reply, but file or filename, these are name of the input, but the selection is file type, so, I think that shouldn't be a problem. This a plain code for media upload, and the user is admin, not editor.

    Screenshot_2023-08-05_at_11.44.52_AM.png
    Screenshot_2023-08-05_at_11.47.15_AM.png
  • default discord avatar
    muperdev
    last month

    can you share your media collection snippet here?

  • default discord avatar
    artmelkon
    last month

    import { CollectionConfig } from "payload/types";;



    const Media: CollectionConfig = {


    slug: 'media',


    admin: {


    useAsTitle: 'title',


    defaultColumns: ['filename', 'id'],


    },


    upload: true,


    fields: [


    {


    name: 'alt',


    type: 'text',


    }


    ]


    }



    export default Media;



    this is the same code as in the screenshot.

  • default discord avatar
    muperdev
    last month

    auth: false,


    access: {


    read: () => true,


    create: () => true


    },



    add this above fields @artmelkon



    just wanted to see if it works without any credentials

  • default discord avatar
    artmelkon
    last month

    still getting 404

    Screenshot_2023-08-06_at_7.50.01_AM.png
    Screenshot_2023-08-06_at_7.54.59_AM.png
  • discord user avatar
    alessiogr
    Payload Team
    last month

    remove the

    /create

    . So just

    http://localhost:4000/api/media

    🙂



    The "POST" automatically means "create"

    Screenshot_2023-08-06_at_17.05.332x.jpg
  • default discord avatar
    artmelkon
    last month

    I've created another test posing a new page. When I use /api/pages/create endpoint it fails, but when I use /api/pages it succeded, it is puzzling but it did work



    Yes, it did work. Thanks

  • discord user avatar
    alessiogr
    Payload Team
    last month
    https://payloadcms.com/docs/rest-api/overview#collections

    check it out this section, it showcases all operations you can do on a collection with the rest API - together with an example and the URL / rest operation you have to use



    Glad it works now!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.