supabase-community / postgrest-go

Isomorphic Go client for PostgREST. (Now Updating)
https://supabase.io
Apache License 2.0
172 stars 27 forks source link

`Execute()` is not sending request to the correct URL #3

Closed marcustut closed 3 years ago

marcustut commented 3 years ago

Bug report

Describe the bug

So when I try to insert data with the following code snippet, I expect it to send a request to https://<supabase_url>/rest/v1/products but it seems like the Execute() function is not sending the request to the correct URL. image

The result of trying to run this is that I get the response as below, after a google search it seems like this is what Kong would respond if it can't find a route

image

So I try to directly change the client.clientTransport.baseURL.Path to https://<supabase_url>/rest/v1/products, the request is sent to the correct place where I can see a postgrest response but the query is still not successfully created yet. I'm suspecting is that the way the querybuilder marshal json is making some errors here.

image

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Get the library
  2. Use a supabase url, anon key and proper auth token to create the client
  3. Try to insert data into a table
  4. See error

Expected behavior

The request should be sent to the correct url and querybuilder should properly marshal value into json

Screenshots

Shown above

System information

Additional context

I will try fixing it and if I do, I will create a PR.

marcustut commented 3 years ago

Looking further into the URL issue, it seems that because local postgrest would run on an url like http://localhost:3000 and therefore the path to a table would be http://localhost:3000/<table_name> but since supabase has its postgrest server at https://<supabase_url>/rest/v1 so the path to a table would be https://<supabase_url>/rest/v1/<table_name>.

But if I directly pass https://<supabase_url>/rest/v1 to the NewClient func, it wouldn't work as well due to how url parsing is implemented, it will result in the following baseURL

image

// So doing the following would result in a correct path

client.From("/rest/v1/products")

// instead of

client.From("products")

probably we should set a func param while creating the client to specify the nested path?

yusufpapurcu commented 3 years ago

No we must be able to give url like first example. I will look at this issue.

yusufpapurcu commented 3 years ago

Can you try adding https:// in your url?

marcustut commented 3 years ago

Can you try adding https:// in your url?

It's already added, this is how I specify it

image

yusufpapurcu commented 3 years ago
Screen Shot 2021-06-03 at 21 58 45

You must define like this. This is just a postgrest library and you must specify postgrest path. Supabase's postgrest runs in rest/v1/. That's cause of your problem 😆

marcustut commented 3 years ago
Screen Shot 2021-06-03 at 21 58 45

You must define like this. This is just a postgrest library and you must specify postgrest path. Supabase's postgrest runs in rest/v1/. That's cause of your problem 😆

Ah.. yea tested it just now, my bad. turns out you need to set https://<supabase_url>/rest/v1/ for it to work instead of https://<supabase_url>/rest/v1

However, I still can't get Insert() to work, still trying to debug it, however Select() works flawlessly.

marcustut commented 3 years ago

I got insert() to work, the problem is with me again 😅 I was passing map[string]interface{} as value which json.Marshal doesn't know how to marshal it into a correct json object.

So I wrote a struct with proper json struct tags that correspond to the table's field name then it worked.

Anyways, I found that the examples is quite lacking, shall I write few more examples under the test/ folder?

yusufpapurcu commented 3 years ago

We will add more examples and you're welcome to contribute 😉