How to post data into collection from plugin frontend?

Hi,

I am trying to update a few fields in a collection based on user selection.
I was able to fetch the data using “request” util from strapi-plugin-helper.
However, I have not been able to find anything to “post” or “put” in the strapi-plugin-helper.

Is there such a utility or some other way?
Or I just have to use axios?

System Information
  • Strapi Version: 3.6.3
  • Operating System: MacOS Big Sur
  • Database: Postgres
  • Node Version: 14
  • NPM Version: 6.x latest
  • Yarn Version: N/A

I would recommend using axios or graphql to make get/post/put requests from a frontend perspective.
No need to try over complicate a simple task such as populating your content

Hi Ben,

It’s not the frontend application. It’s the Strapi admin frontend.

So i figured out that “request” is just a wrapper over fetch API, so we can do a PUT or POST by supplying options.
However now I am getting - Method Not Allowed (Error 405)
I have checked the permissions and roles, I have granted all the permissions.

Any ideas?

Code -

const data = {
      status: "completed"
    };
    await request(
      `/verification/${id}`,
      {
        method: 'PUT',
        headers: {
          'Content-Type': 'text/plain'
        },
        body: data
      },
    );

Ok I am dumb.
My collection name was verifications.
But in the URL I was using /verification/${id}, it should be /verifications/${id}

Just want to leave this out there, cause it took me some time to figure out.
To get and update collections from your plugin component, do this

import { request } from 'strapi-helper-plugin';

const getCollectionData = async () => {
  const data = await request("/collections?<filter-conditions>");
  return data;
}

const updateCollectionData = async (id) => {
  const data = {
      fieldA: "Hello World"
    };
    await request(
      `/collections/${id}`,
      {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json'
        },
        body: data
      },
    );
    console.log("collections updated");
}

1 Like