How to send request to another server in shelf dart

Issue

I need to make a request to my firebase rtdb from my shelf server hosted on 127.0.0.1, I have the url and the db secrets. But whenever i try to make a get request to the db url using the http package, i get a 401 error.

My code:

import 'dart:io';
import 'package:http/http.dart';
import 'package:firebase/firebase_io.dart';

class FirebaseLocalClient {
  void putSudokuBoard() async {
    var a = await get(
        Uri.parse(
            "<db url>"),
        headers: {
          "Authorization": "Bearer <your database secret>",
          'Content-Type': "application/js"
        });

    print(a.statusCode);
    //print(a.runtimeType);
  }
}

void main(List<String> args) {
  FirebaseLocalClient().putSudokuBoard();
}

I call this code from a shelf server(similar to the code in main function), but running it here itself recieves a 401 error.
I am not able to understand why i am recieving a 401 error, i have the db secrets and yet i am unable to get the data at that location. I tried using the admin sdk json but recieved 401 on that too

The output when i use a.body:
enter image description here

The output when i use a.statuscode:
enter image description here

Solution

If you are using the db secrets, it looks like you need to append the auth param.
per https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-uri-params

curl 'https://docs-examples.firebaseio.com/auth-example.json?auth=CREDENTIAL'

Remove the Authorization header and try it in curl

Answered By – aaronvargas

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.