S
Supabase2y ago
Sam

How to use the HTTP API For Supabase to GET / POST data and return records?

I am struggling to use the Supabase http api, and looking for some more detailed documentation — for example, the required headers for different types of requests. (It took me a long time of searching to realize that a 'PREFER' header is required to try and return a record while inserting, is there any official documentation for requirements like that? As an example, in a Netlify function, I am trying to make a POST request work. When the function is structured like this:
const response = await fetch(`${process.env.SUPABASE_URL}/rest/v1/posts?select=*`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apikey': process.env.SUPABASE_API_KEY,
'Authorization': `Bearer ${process.env.SUPABASE_API_KEY}`,
},
body: JSON.stringify(newData),
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
const response = await fetch(`${process.env.SUPABASE_URL}/rest/v1/posts?select=*`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apikey': process.env.SUPABASE_API_KEY,
'Authorization': `Bearer ${process.env.SUPABASE_API_KEY}`,
},
body: JSON.stringify(newData),
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
It successfully creates a new record in the specified table, but returns an invalid response. When I try this function instead, using the Prefer header
const response = await fetch(`${process.env.SUPABASE_URL}/rest/v1/posts?select=*`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apikey': process.env.SUPABASE_API_KEY,
'Authorization': `Bearer ${process.env.SUPABASE_API_KEY}`,
'Prefer': 'return=representation',
},
body: JSON.stringify(newData),
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
const response = await fetch(`${process.env.SUPABASE_URL}/rest/v1/posts?select=*`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'apikey': process.env.SUPABASE_API_KEY,
'Authorization': `Bearer ${process.env.SUPABASE_API_KEY}`,
'Prefer': 'return=representation',
},
body: JSON.stringify(newData),
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
I get the following error, which doesn't appear when making the same http request just without the 'Prefer' header.
{
code: '42501',
details: null,
hint: null,
message: 'new row violates row-level security policy for table "posts"'
}
{
code: '42501',
details: null,
hint: null,
message: 'new row violates row-level security policy for table "posts"'
}
Can someone help me with some more detailed and simple code examples for the http api, such as inserting a record and returning the result, getting records from a table, getting records with a filter, and getting records with a foreign key reference included?
2 Replies
garyaustin
garyaustin2y ago
PostgREST
API
PostgREST exposes three database objects of a schema as resources: tables, views and stored procedures. Tables and Views, Stored Procedures, Schemas, Computed Fields, Domain Representations, Pagination and Count, Resource Embedding, Resource Representation, Media Type Handlers, Aggregate Function...
Sam
SamOP2y ago
I had totally missed that, thank you so much @garyaustin, I should have taken a closer look. Really appreciate it, and hope you're doing well and having a nice end of year. thank you again.

Did you find this page helpful?