0

In postman I issue a request with the following body against http://localhost:2001/api/ProductQuality/Check:

{
    "productid": "2274",
    "productcorenumber": "1321",
    "value": "AE1C1F-0363-D6F-B3D-AE0029A8",
    "contacts": [
        15998,
        1012
    ],
    "ispassed": "0",
    "isok": "0",
    "isgood": "false"
}                   

and this will come to here(I have one method in my controller)

[HttpPost("api/ProductQuality/Check")]
public async Task<IActionResult> Check([FromBody] CheckInput input)
{
    var result = await _Checker.Checkercall(input);
}

in Checkercall implementation I am calling another API(as of now calling the same for testing purposes)

public async Task<QualityCheckResults> Checkercall(CheckInput input)
{
    QualityCheckResults QualityCheckInputResult = new QualityCheckResults();
    using (var httpClient = new HttpClient())
    {
        var content = new StringContent(JsonConvert.SerializeObject(input), Encoding.UTF8, "application/json");
        using (var response = await httpClient.PostAsync("https://localhost:2001/api/ProductQuality/Check", content))
        //hope i have to give https here
        {
            string apiResponse = await response.Content.ReadAsStringAsync();
            QualityCheckInputResult = JsonConvert.DeserializeObject<QualityCheckResults>(apiResponse);
        }
    }
    return QualityCheckInputResult;
}

I am getting Unhandled Exception here but I couldn't understand the issue.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 1
    From what I understand, you are doing recursive API call without an exit condition? You'll get out of memory exception and/or thread starvation because there is no exit condition – scharnyw Feb 04 '21 at 08:48
  • is that the problem causing my exception.Am new to this so i wonder how i can fix this – beginer_programer Feb 04 '21 at 08:58
  • I can't tell without information like the exception details or call stack. Your API business logic should not be calling the API itself, even for testing. This leads to recursion. Instead you could implement a different API (e.g. /api/ProductQuality/Check/mock) which imitates the behavior of the real external API for testing purposes and call it instead. – scharnyw Feb 04 '21 at 09:02
  • Could not cast or convert from System.String to .NetCore.Core.Models.Response.CheckResults. – beginer_programer Feb 04 '21 at 09:18
  • You should look at `ADOCheckerAsync.cs` line 52. You are getting a deserialization error here. My guess is that you are trying to deserialize a JSON string into a .NET object (a `CheckResults` instance) – scharnyw Feb 04 '21 at 09:30
  • i shouldnt do like that? – beginer_programer Feb 04 '21 at 10:48
  • No. You can deserialize a JSON object into .NET object, but without custom JSON conversion logic, you cannot deserialize a JSON string into .NET object by default – scharnyw Feb 04 '21 at 12:46
  • and when i debug in the response (before coming to deserialzation) i am getting the bad request error – beginer_programer Feb 04 '21 at 13:14
  • Because input JSON serialization errors are treated as model state errors by the framework which results in 400 response. See [this](https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-5.0#automatic-http-400-responses) – scharnyw Feb 04 '21 at 14:41
  • i just want to return the responses(a json obj) by this httpclient call,but i am unable to find a solution – beginer_programer Feb 04 '21 at 15:42

0 Answers0