Home > OS >  How ro Return a boolean in Get Response body
How ro Return a boolean in Get Response body

Time:10-12

I'm writing a get API method: at the moment my API returns success 200 if the result is true and if it's false return 400. But in response response body I'm getting no content 200: I'm trying to return success True with 200 success : API :

[SwaggerResponse(HttpStatusCode.OK)]
[HttpGet]
[Route("tenants/{tenantId:Guid}/cpgofferstatus/{advertOfferId:Guid}")]
public async Task<HttpResponseMessage> statusCheck([FromUri] Guid tenantId, [FromUri] Guid advertOfferId)
{
   var result = await _digitalOffersDataProvider.CpgOfferStatus(tenantId, advertOfferId).ConfigureAwait(false);
           
   return Request.CreateResponse(result ? HttpStatusCode.OK : HttpStatusCode.BadRequest);
}

Data Provider Logic:

public async Task<bool> CpgOfferStatus(Guid tenantId, Guid advertOfferId)
{
    try
    {   
        var result= await _offerRepository.CpgOfferStatus(tenantId, advertOfferId).ConfigureAwait(false);
        return result;
    }
    catch (Exception ex)
    {
        //"throw some exception, Intentionally I didn't add here  "
    }
    //...
}

API response:

Response Body
no content
Response Code
200

My intention is to return Response Body as True

CodePudding user response:

Try this

return result ? 
Request.CreateResponse(HttpStatusCode.OK, true) : 
Request.CreateResponse(HttpStatusCode.BadRequest, false);
  • Related