Search code examples
c#posthttp-headerspostmanbasic-authentication

PostAsJsonAsync C# - How to set headers correctly for POST request - BAD REQUEST 400


I am trying to do a POST request but when I am compiling and executing it with the debugger of Visual Studio Code, I am getting an error of 400 Bad Request. Regardless of that, when I am doing the same POST request in Postman, I am getting a 200 OK status request with all the values that I need for continuing to the next part in which I am working on. enter image description here

Moreover, there is a Basic Auth in the request which in this case I am including it in Postman and it works fine. From the other side, in my script with C#, I am executing it like this:

*This is my model in which all my data is included for serializing the object into JSON.

 public class patient{
    public string deviceId { get; set; }        
    public string deviceType { get; set; }
    public string apiVersion { get; set; }
    public string language { get; set; }
    public externUserClass externUser{ get;set; }

    public class externUserClass{
        public externUserClass(string partnerExternalId, string username, string newPassword, string gender){
            this.partnerExternalId = partnerExternalId;
            this.username = username;
            this.newPassword = newPassword;
            this.gender = gender;
        }
        public string partnerExternalId { get; set; }
        public string username { get; set; }
        public string newPassword { get; set; }
        public string gender { get; set; }
    }
    public string includeAuthToken{ get; set; }
}

*This is my Helper class for creating the POST request. I insert all the data that I need and then I serialize the object to JSON as some of you have adviced me to do so. It is quite cleaner.

 public async Task<string> HubCreateUser(string conf, string userId, string sexo, string deviceId)
    {
        var sexoStr = "";  
        if(sexo == "MALE") {
            sexoStr = "MALE";
        } else if(sexo == "FEMALE") {
            sexoStr = "FEMALE";
        }

        var guid = Guid.NewGuid().ToString(); // guid para el username y el password
        var data = new patient();
        data.deviceId = userId;
        data.deviceType = "WEB";
        data.apiVersion = "4.0.3";
        data.language = "es_ES";
        data.externUser = new patient.externUserClass(userId, guid, guid, sexoStr); // extern user
        data.includeAuthToken = "true";

        string output = JsonConvert.SerializeObject(data);

        var severDestination= conf;
        var client =  new HttpClient{BaseAddress = new Uri(severDestination)};
        MediaType = "application/json";
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaType)); //ACCEPT header
        client.DefaultRequestHeaders.Authorization =  new AuthenticationHeaderValue("xxxxxx", "xxxxxxxxxxxx");
        var Client = client;
        var request = await Client.PostAsJsonAsync("externUser", output);

        request.EnsureSuccessStatusCode();
        var status = await request.Content.ReadAsStringAsync();
        return status;
    }

I did serialize the object and I am getting it the way I need to. But, whenever it comes to the request, it gives me a 400 Bad Request.


Solution

  • Try with PostAsync instead of PostAsJsonAsync

    var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
    
    var request  = await httpClient.PostAsync(requestUrl, content);
    

    Please find more details on PostAsJsonAsync at HttpClient not supporting PostAsJsonAsync method C#