Search code examples
restrestsharp

Verify API Response


Am getting the response of a request like this: var response = command.PostCommand(testCommand);

I will like to validate that the response is in a json format so am doing it like this:

Assert.AreEqual("application/json", response.ContentType);

Is this way correctly or do i need to specifically validate it from the content-type header response?


Solution

  • You can use the IRestRequest.OnBeforeDeserialization callback to check the response content type before it gets deserialised:

    var request = new RestRequest(url)
        .AddQueryParameter(x, y); // whatever you need to configure
    
    request.OnBeforeDeserialization = 
        response => CheckContentType(response.ContentType);
    
    await client.PostAsync<MyResponse>(request);