fair-rose
fair-rose7mo ago

JSON from form-data

create: async ({ resource, variables, meta }) => { console.log(variables); let url = ${apiUrl}/${resource}/create; if (resource === "movie production member role") { url = ${apiUrl}/movieproductionmemberrole/create; } const { headers, method } = meta ?? {}; const requestMethod = (method as MethodTypesWithBody) ?? "post"; let mutatedata; if (resource === "movie production member role") { mutatedata = variables; } else { const { id, uuid, created_at, updated_at, edges, ...rest } = variables; mutatedata = JSON.stringify(rest); } console.log(mutatedata); const { data } = await httpClient[requestMethod](url, mutatedata, { headers: { ...headers, "Content-Type": "multipart/form-data", }, }); return { data: data.data, }; }, how do i send JSON data from form-data @kapa.ai
2 Replies
kapa.ai
kapa.ai7mo ago
Hi @Forever Knight. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To send JSON data from form-data, you can use the create method in your data provider. In the create method, you can handle the request according to your API requirements. Here is an example of how you can do it:
create: async ({ resource, variables, meta }) => {
console.log(variables);
let url = `${apiUrl}/${resource}/create`;
if (resource === "movie production member role") {
url = `${apiUrl}/movieproductionmemberrole/create`;
}
const { headers, method } = meta ?? {};
const requestMethod = (method as MethodTypesWithBody) ?? "post";
let mutatedata;
if (resource === "movie production member role") {
mutatedata = variables;
} else {
const { id, uuid, created_at, updated_at, edges, ...rest } = variables;
mutatedata = JSON.stringify(rest);
}
console.log(mutatedata);

const { data } = await httpClient[requestMethod](url, mutatedata, {
headers: {
...headers,
"Content-Type": "multipart/form-data",
},
});

return {
data: data.data,
};
},
create: async ({ resource, variables, meta }) => {
console.log(variables);
let url = `${apiUrl}/${resource}/create`;
if (resource === "movie production member role") {
url = `${apiUrl}/movieproductionmemberrole/create`;
}
const { headers, method } = meta ?? {};
const requestMethod = (method as MethodTypesWithBody) ?? "post";
let mutatedata;
if (resource === "movie production member role") {
mutatedata = variables;
} else {
const { id, uuid, created_at, updated_at, edges, ...rest } = variables;
mutatedata = JSON.stringify(rest);
}
console.log(mutatedata);

const { data } = await httpClient[requestMethod](url, mutatedata, {
headers: {
...headers,
"Content-Type": "multipart/form-data",
},
});

return {
data: data.data,
};
},
In this code, variables is the data you want to send. If the resource is "movie production member role", it sends the variables as is. Otherwise, it removes id, uuid, created_at, updated_at, and edges from variables, converts the rest to a JSON string, and sends that. The Content-Type header is set to "multipart/form-data", which is necessary when you're sending form data. You can learn more about the create method in the Refine documentation (https://refine.dev/docs/data/data-provider#create-). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7mo ago
<@484585406495588352> kapa.ai is still learning and improving, please let us know how it did by reacting below