question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Post with Authentication Results in Error 400 (Bad Request)

See original GitHub issue

I’ve got some pretty simple code which should create a new ClassRoom object and save it to Firebase under “classRooms”. If I change my authentication rules in the database to allow universal access and I remove the authentication part of the request, everything works fine. So I think the issue has something to do with authentication.

Here’s the code which makes the request:

String id = subjectId.Text;
String name = subjectName.Text;
String activity = this.activity.Text;

var classRoom = await firebase
            .Child("classRooms")
            .WithAuth(auth.FirebaseToken)
            .PostAsync<ClassRoom>(new ClassRoom(id, name, activity));

And here’s the ClassRoom class:

class ClassRoom
    {
        public String subjectId;
        public String subjectName;
        public String activity;

        public ClassRoom(String subjectId, String subjectName, String activity)
        {
            this.subjectId = subjectId;
            this.subjectName = subjectName;
            this.activity = activity;
        }
    }

Thanks so much for any time anyone puts in to this. Your efforts are much appreciated 👍

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16

github_iconTop GitHub Comments

2reactions
bezysoftwarecommented, Nov 10, 2016

Hi folks, sorry for taking so long to reply, I was on vacation. Anyway, the solution mentioned by @jaybowman will work, however that’s not the preferred way of doing it. I will actually obsolete the “WithAuth” method and remove it later completely.

The preferred way of doing auth is using the FirebaseClient’s constructor with FirebaseOptions like this:

            var firebaseClient = new FirebaseClient(
                "<URL>",
                new FirebaseOptions
                {
                    AuthTokenAsyncFactory = () => Task.FromResult("...") // can also be async call
                });

I will amend the docs for auth to reflect this.

1reaction
jaybowmancommented, Nov 2, 2016

Here is a workaround, generate the key outside the postAsync() .

string key = Firebase.Database.FirebaseKeyGenerator.Next();
   // insert this works, creates a correctly formated URL
   await _client.Child("homes")
                       .Child(key)
                       .WithAuth(token)                              
                       .PostAsync<Home>(myhome, false);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix a 400 Bad Request Error (Causes and Fixes)
The 400 Bad Request error indicates that the server cannot or process the request due to a client error. Read about the common...
Read more >
How to Fix a 400 Bad Request Error: 8 Easy Methods
1. Double Check the Domain Address. One of the most common causes of HTTP status 400 bad request error is a wrong URL....
Read more >
POST 400 bad request React authentication
It will be good for you if you console the input, because 400 bad request is related to input and out error (wrong...
Read more >
400 Bad Request Error: What It Is and How to Fix It
If an expected custom HTTP header is missing or invalid, a 400 error is a likely result. The client may be uploading a...
Read more >
400 Bad Request - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found