RestClient icon indicating copy to clipboard operation
RestClient copied to clipboard

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

Open kiritodragneel opened this issue 1 year ago • 7 comments

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 = "[email protected]";
        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 = "[email protected]";
        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);
        }
    }

kiritodragneel avatar Jul 05 '24 19:07 kiritodragneel