Home > database >  API 400 error Username required Password required
API 400 error Username required Password required

Time:09-13

I am sending JSON data to external API as following :

Dim url = "https://********"
    Dim _httpClient = New HttpClient()
    _httpClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("MyUsername", "MyPassword")

    Using content = New StringContent(json, System.Text.Encoding.UTF8, "application/json")
        Dim result As HttpResponseMessage = _httpClient.PostAsync(url, content).Result
                    If result.StatusCode = System.Net.HttpStatusCode.Created Then
            Console.WriteLine(result.StatusCode)
            Return True
        End If

but I am receiving the following error:

{"status":400,"message":"Invalid Request","data":[{"field":"username","message":"Username required"},{"field":"password","message":"Password required"}]}

how to fix this issue ?

CodePudding user response:

As per your API definition it is expecting username and password in the header as key value pair. you can add them like this.

_httpClient.DefaultRequestHeaders.Add("Username", "MyUsername");
_httpClient.DefaultRequestHeaders.Add("password", "MyPassword");
  • Related