rcn1
March 24, 2025, 3:16pm
1
I am using a Dedicated Dgraph Cloud instance and want to query it via the dgo client. I passed the JWT that I obtained using the login query (from
https://cerebro.cloud.dgraph.io/graphql ) in the authorization header.
The client is being created but the DQL query fails with the following error:
rpc error: code = Unauthenticated desc = unexpected HTTP status code received from server: 401 (Unauthorized); malformed header: missing HTTP content-type
Here is the Go code:
func ConnectToDgraph() {
conn, err := dgo.DialCloud(dgraphCloudURL, apiKey)
if err != nil {
log.Fatalf("failed to connect: %v", err)
}
dc := api.NewDgraphClient(conn)
dg := dgo.NewDgraphClient(dc)
query := `
{
all(func: has(name)) {
uid
name
}
}`
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", "Bearer "+token))
resp, err := dg.NewTxn().Query(ctx, query)
if err != nil {
log.Fatalf("Query failed: %v", err)
}
fmt.Println("Response from Dgraph:", string(resp.Json))
}
Am I missing something?
Thanks in advance.
Can you add to the metadata:
-H ‘Content-Type: application/json’
along with the --header
rcn1
March 24, 2025, 3:38pm
3
Tried adding that but the error still remains unresolved
Actually, with dgo, could you try logging in using the .Login() and LoginIntoNamespace() functionality.
rcn1
March 24, 2025, 4:35pm
5
Logged in using
dg.Login(context.Background(), os.Getenv("userid"), os.Getenv("password"))
The login was successful but the error message remains the same. Should I be using dg.GetJwt().AccesJwt
for the authorization header?
Could you try this and copy the response you are getting?
func ConnectToDgraph() {
conn, err := dgo.DialCloud(dgraphCloudURL, apiKey)
if err != nil {
log.Fatalf("failed to connect: %v", err)
}
dc := api.NewDgraphClient(conn)
dg := dgo.NewDgraphClient(dc)
err = dg.Login(context.Background(), os.Getenv("userid"), os.Getenv("password"))
if err != nil {
log.Fatalf("Login failed: %v", err)
}
query := `
{
all(func: has(name)) {
uid
name
}
}`
resp, err := dg.NewTxn().Query(ctx, query)
if err != nil {
log.Fatalf("Query failed: %v", err)
}
fmt.Println("Response from Dgraph:", string(resp.Json))
}
rcn1
March 25, 2025, 1:02am
7
@amanmangal this is working as expected. Thank you!
system
(system)
Closed
March 30, 2025, 4:30am
8
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.