Sergi
October 22, 2023, 1:12pm
1
System Information
Strapi Version: 4.14 :
Operating System: Macos Monterey :
Database: Postgresql :
Node Version: v20 :
So basically i am trying to create a custom plugin. i use useFetchClient(). get function works ok, but when i do a POST request, i can’t get the data to be sent via req, as you can see from the screenshot.
svenali
January 25, 2024, 10:56pm
3
Hi Sergi,
I get the same result like you, do you have a solution found?
regards,
Sven
Sergi
January 30, 2024, 11:18pm
4
I think i managed to make it work using the older hook getFetchClient or axios. Anyway somehow after hours of testing, they both worked. Maybe check your version
1 Like
Yeah I would double check your Strapi version. I used getFetchClient and it worked. Here is an example file for reference just in case. https://github.com/PaulBratslavsky/strapi-plugin-open-ai-embeddings/blob/main/admin/src/pages/CreateEmbeddings/index.jsx
Have the same issue on v4.14.6.
I send data with:
const res = await client.post('/gfw-custom-fields/upload-asset', {
data: {
foo: 'BAR',
},
});
But on the server side ctx.request.body
is undefined
Just tested it with getFetchClient
instead of useFetchClient
and there the post body is available on the request.
Hello there,
I can use both now. getFetchClient is marked as deprecated, so it will be removed in the near future. So here my solution that works:
In my Homepage (src/plugin/admin/src/pages):
import { useFetchClient } from '@strapi/helper-plugin';
import pluginId from '../../pluginId';
const HomePage = () => {
const { get, post } = useFetchClient();
...
const testSQLJob = () => {
let kindOfTest = modus ? "findOne" : "findMany";
const testJob = post("/sqljob/test-job", { data: {
entity: value,
modus: kindOfTest,
json_content: content
}
}).then((response) => {
console.log("TestJob: ", response.data);
setJsonPreview(response.data);
});
}
The route (src/plugin/server/routes):
{
method: 'POST',
path: '/test-job',
handler: 'myController.testJob',
config: {
policies: [],
},
},
The controller (src/plugin/server/controllers):
async testJob(ctx) {
ctx.body = await strapi
.plugin('sqljob')
.service('myService').testJob(ctx.request.body.data);
},
In the service (src/plugin/server/services)I return just a findMany - Result, for instance:
let response = await strapi.entityService.findMany(data.entity, json_object);
...
return response;
Thank you all!
regards,
Sven
not safe.
means reachable from outside.
must be ;
auth: {
type: “admin”, // or “user”
},