-
Hi, I'm trying to create a custom object in a custom application, It throws errors like 400, 401, and 406. Can you please suggest how can I resolve this error?
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey @nikhilkni, Your code is throwing HTTP 400, 401, and 406 errors due to potential issues with the request structure, authentication, and missing or misconfigured headers.
Also, the res.json() is a promise and should be awaited, like this: Lastly, don't forget to provide the required headers (Accept-version, X-Forward-To, X-Forward-To-Audience-Policy, X-Project-Key) when using the /proxy/forward-to endpoint. If you're still having trouble, it would be helpful to see the exact error message and potentially more context about the request you're making. |
Beta Was this translation helpful? Give feedback.
-
Hey @nikhilkni, Kindly try this snippet and let us know if it works for you. const fetcher = async (url, config = {}) => {
let headers;
const data = await executeHttpClientRequest(
async (options) => {
headers = { ...options.headers, ...config.headers };
const res = await fetch(buildApiUrl(url), { ...config, headers });
const data = res.json();
return {
data,
statusCode: res.status,
getHeader: (key) => res.headers.get(key),
};
},
{ headers }
);
return data;
};
export const createCustomObject = async () => {
try {
const customObject = await fetcher(`/${projectKey}/custom-objects`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
container: 'customer_preferences',
key: 'notification_settings',
value: {
email_notifications: true,
push_notifications: false,
sms_notifications: true,
preferred_language: 'en',
},
}),
});
console.log('Custom object created:', customObject);
} catch (error) {
console.error('Error creating custom object:', error.message);
}
}; Please make sure you follow guidelines from the data fetching section in docs. |
Beta Was this translation helpful? Give feedback.
Hey @nikhilkni,
Kindly try this snippet and let us know if it works for you.