Home > Net >  How to I get the detail (custom error message) returned with a bad request status code? So that I ca
How to I get the detail (custom error message) returned with a bad request status code? So that I ca

Time:10-11

Hi so I am setting up some Integration tests (using Xunit) and I would like to run an Assert to check whether the correct custom error message is returned.

This is the data I need to get is in the following response see image... detail: "Username must be unique" Don't worry this message will be modified to be more useful later on I am just wanting to get it working first

Required Info

This is the current code...

        //Act

        response = await _httpClient.PostAsync("CompleteUserSetup", formContent);

        //Assert

        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode) ; //Bad request should be returned
        //TODO: check custom error message is correct

So hoping for...

ASSERT.Equal("Username must be unique", some code to get detail from response)

CodePudding user response:

Okay so I figured out how to get the data I needed. I just needed to convert the result into an object and then I was able to pull the detail data that I needed.

var resultModel = await System.Text.Json.JsonSerializer.DeserializeAsync<Result>(response.Content.ReadAsStream(), JsonSerializerHelper.DefaultDeserialisationOptions);

var errorMessage = resultModel.detail;
  • Related