Skip to content

Directus Auth - 400 bad request using rest client but functional old school way?? #248

@kiritodragneel

Description

@kiritodragneel

Hey, so I am trying to connect and get an access Token from my directus instance.
If I use this code the code below then I get a 400 bad request error.

private async Task AuthenticateUserRest() {
        var url = "https://directus.mydomain.uk/auth/login";
        var email = "myemail@mydomain.com";
        var password = "Mysupersecurepassword!";
        var mode = "cookie";
        
        //Tried the json as a string seen in below example, made no difference at all
        JSON requestBody = new JSON();
        requestBody.Add("email", email);
        requestBody.Add("password", password);
        requestBody.Add("mode", mode);
        
        RestClient.Request(new RequestHelper {
            Uri = url,
            Method = "POST",
            Headers = new Dictionary<string, string> {
                { "Accept", "application/json, text/plain, */*" }
            },
            ContentType = "application/json",
            Body = requestBody,
            Retries = 32
        }).Then(response => {
            if (response.StatusCode == 200) {
                Debug.Log("Response data: " + response.Text);
                var responseJSON = JSON.ParseString(response.Text);
                TOKEN = responseJSON.GetJSON("data").GetString("access_token");
                RestClient.DefaultRequestHeaders["Authorization"] = "Bearer " + TOKEN;
            } else {
                Debug.Log("Error data: " + response.Text);
            }
        }).Catch(err => {
            Debug.LogError("Error: " + err.Message);
        });
    }

However if i use this snippet then it works without issue

 private async Task AuthenticateUser() {
        var url = "https://directus.mydomain.uk/auth/login";
        var email = "myemail@mydomain.com";
        var password = "mysupersecurepassword!";
        var mode = "cookie";
        var json = $"{{\"email\":\"{email}\",\"password\":\"{password}\",\"mode\":\"{mode}\"}}";
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "application/json, text/plain, */*");

        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);

        if (response.IsSuccessStatusCode){
            var responseData = await response.Content.ReadAsStringAsync();
            Debug.Log("Response data: " + responseData);
            JSON _responseJSON = JSON.ParseString(responseData);
            TOKEN = _responseJSON.GetJSON("data").GetString("access_token");
        }else{
            Console.WriteLine("Error: " + response.StatusCode);
            var errorData = await response.Content.ReadAsStringAsync();
            Debug.Log("Error data: " + errorData);
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions