Unable to Authenticate DQL Queries Using dgo in Dgraph Cloud

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

Tried adding that but the error still remains unresolved

Actually, with dgo, could you try logging in using the .Login() and LoginIntoNamespace() functionality.

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))
}

@amanmangal this is working as expected. Thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.