Home > Blockchain >  API Returns OK in API but no Data
API Returns OK in API but no Data

Time:01-05

I'm trying to return an object in a model through another API I've made but when I fetch my API with GET on PostMan it only returns 200 OK but an empty array.

This is what I'm trying to get:

[
  {
    "productId": 0,
    "quantity": 0
  }
]

And this is what I'm getting in PostMan

[]

by calling with this API URL:

http://localhost:5700/api/Orders/basket/firstId

Here is my controller and the respective GET method on which I'm calling upon in Postman:

[HttpGet("basket/{identifier}")]
public async Task<IEnumerable<BasketEntryDto>> FetchBasketEntries(string identifier)
{
    var httpRequestMessage = new HttpRequestMessage(
       HttpMethod.Get,
       $"https://localhost:5500/api/Basket/{identifier}")
    {
        Headers = { { HeaderNames.Accept, "application/json" }, }
    };

    var httpClient = httpClientFactory.CreateClient();

    using var httpResponseMessage =
        await httpClient.SendAsync(httpRequestMessage);

    var basketEntires = Enumerable.Empty<BasketEntryDto>();

    if (!httpResponseMessage.IsSuccessStatusCode)
        return basketEntires;

    using var contentStream =
        await httpResponseMessage.Content.ReadAsStreamAsync();

    var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

    var basketDTO = await JsonSerializer.DeserializeAsync
            <BasketDto>(contentStream, options);

    //basketDTO = new NewBasketDTO.ItemDTO
    //{
    //    ProductId = basketDTO.ProductId,
    //    Quantity = basketDTO.Quantity
    //};

    basketEntires = basketDTO.Entries.Select(x =>
        new BasketEntryDto
        {
            ProductId = x.ProductId,
            Quantity = x.Quantity
        }
    );

    return basketEntires; // 200 OK
}

Here is my BasketDTO:

public class BasketDto
{
    public string Identifier { get; set; }
    public IEnumerable<BasketEntryDto> Entries { get; set; } = new List<BasketEntryDto>();
}

and my BasketEntryDto:

public class BasketEntryDto
{
    public int ProductId { get; set; }
    public int Quantity { get; set; }
}

and this is the original API in JSON:

{
    "identifier": "mrPostMan",
    "items": [
        {
            "productId": 1,
            "quantity": 1
        }
    ]
}

in which I want to get the items array and its object.

Is there something I'm doing wrong? Why is it returning an empty array? Thanks in advance for any help..

CodePudding user response:

As I mentioned in the comments, you need to change the Entries property in BasketDTO to Items to match with the JSON property name.

public class BasketDto
{
    public string Identifier { get; set; }
    public IEnumerable<BasketEntryDto> Items { get; set; } = new List<BasketEntryDto>();
}

Alternatively, you could also explicitly mention the JSON Property name by using JsonPropertyNameAttribute

public class BasketDto
{
    public string Identifier { get; set; }

    [JsonPropertyName("items")] 
    public IEnumerable<BasketEntryDto> Entries { get; set; } = new List<BasketEntryDto>();
}

CodePudding user response:

Well this will work when there are more than 0 items (basket is not empty) but not when the basket is empty as:

basketEntires = basketDTO.Entries.Select(x =>
        new BasketEntryDto
        {
            ProductId = x.ProductId,
            Quantity = x.Quantity
        }
    );

there are no items, select will not work. so you can do like this:

if(basketEntires.Count == 0)
{
     basketEntires = new BasketEntryDto 
         { 
              ProductId = 0,
              Quantity  = 0 
         }
}

return basketEntires; // 200 OK

And don't forget to add .ToList():

basketEntires = basketDTO.Entries.Select(x =>
    new BasketEntryDto
    {
        ProductId = x.ProductId,
        Quantity = x.Quantity
    }
).ToList();

You should not return IEnumerable, instead you should return list (or array).

  •  Tags:  
  • Related