Click here to Skip to main content
16,018,442 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I am a Web Api beginner am trying to call one api in another api in asp.net core 3.1. I created two solutions with diffrent port numbers. From httpclient method breakpoint is not going to external api methode.its returning a response as bad request please someone tell what i did wrong here?


getting like::::
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers: { Date: Fri, 05 Feb 2021 09:54:45 GMT Server: serverA Content-Type: application/json; charset=utf-8 Content-Length: 35 }


I tried your all httpclient method but I am getting the same error bad request..let me explain what I was trying.

What I have tried:

I have 2 solutions here with having different port numbers..

in 1st solution I have a method
C#
[HttpPost("api/Product/Check1st")]  
public async Task Check([FromBody] CheckInput input)  
{  
    var result = await _productqualityChecker.CheckQualityAsync(input);  
    return new JsonResult(result);  
}  

I will keep running this solution . The url here ***api/Product/Check1st***" I will trying to call using httpclient in 2nd solution.so this url I am not giving in postman .

In my 2nd solution I have a method
C#
[HttpPost("api/ProductQuality/Checkin2nd")]{}

this 2nd url I will give to postman and getting the input(arguments to call external api) and then it will call the httpclient as shown as below

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

This is the body/input/argument I am passing in the postman
{
    "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)
C#
[HttpPost("api/ProductQuality/Checkin2nd")]  
public async Task 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)
C#
public async Task 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/Check1st", content))  
        {  
            string apiResponse = await response.Content.ReadAsStringAsync();  
            QualityCheckInputResult = JsonConvert.DeserializeObject(apiResponse);  
        }  
    }  
    return QualityCheckInputResult;  
}  

This is what I am trying to do. in httpclient mtd CheckInput is the class I have created.
C#
QualityCheckResults QualityCheckInputResult = new QualityCheckResults();
is the response class. So I can assign to this object only. May be that is the issue? I don't know

Is there anything wrong in my 2 solution call?
Posted
Updated 12-Sep-22 6:00am
v2

1 solution

Bad request is likely to be the JSON message expected by the API doesn't match the one actually being sent. If this is the case, a breakpoint probably won't work as the object passed as a parameter into the Check method can't be created from the JSON. I say probably, but it depends exactly where your breakpoint is...

The big question is what does CheckInput actually look like as a type - if it doesn't conform to the JSON it won't work. My eye is drawn to the JSON you send via postman:

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


productid, productcorenumber are serialized as strings, but look like ints, similarly isgood is a string but looks like a bool, finally
ispassed
and
isok
have string values representing ints, but look like they are ought to be bools also. My guess is either some custom work is being done for deserialization but not on serialization or vice versa - unless the properties on the CheckInput are actually strings.

If the object is using the expected data types (int and bool), without custom serialization the json should look more like:

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


There are also different casing strategies for the property names, this might be fouling you up.

If you could add the type you are serializing/deserializing to you'll get more definite help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900