Error with API testing - POST request

I am doing some API testing using testrigor and got this error when sending a POST request.My sample application is hosted in Azure.

API call to ‘https://demotestapp-backend-dev.azurewebsites.net/api/students’ returned HTTP code ‘400’ but expected code is ‘200’ for call api post “https://demotestapp-backend-dev.azurewebsites.net/api/students” with headers “Content-Type:application/json” and “Accept:application/json” and body “{"firstName":"James","lastName":"Warner","isActive":"true","gender":"1"}” and then check that http code is 200

My script is,
call api post "https://demotestapp-backend-dev.azurewebsites.net/api/students" with headers "Content-Type:application/json" and "Accept:application/json" and body "{\"firstName\":\"James\",\"lastName\":\"Warner\",\"isActive\":\"true\",\"gender\":\"1\"}" and then check that http code is 200

Can anyone give a sugggestion to solve this please?

HTTP 400 means there is something wrong in your request. Ideally, the API should give you more details in the response - does the response have a body? Also, is there some documentation you can refer to?

I don’t have access to the API’s documentation so I can just guess. What looks weird in your request body is that you’re sending "true" and "1" both as string datatype. I would expect true to be boolean and 1 to be an integer. So just try to leave out those quotation marks and see what will happen.

2 Likes

Actually, I just tried it myself from Postman and yep, it was exactly that :slight_smile:

curl --location --request POST 'https://demotestapp-backend-dev.azurewebsites.net/api/students' \
--data-raw '{
    "firstName":"James",
    "lastName":"Warner",
    "isActive":true,
    "gender":1}'

returns HTTP 200 OK.

Also, if you’d check the response body that was returned with your HTTP 400, it will tell you exactly what was wrong:

        "$.isActive": [
            "The JSON value could not be converted to System.Boolean. Path: $.isActive | LineNumber: 3 | BytePositionInLine: 21."
        ]

(I was actually wrong about the gender field, it accepts both string and integer - nice resilience!)

3 Likes

@testrigor testrigor - If I remember you did a lot of testrigor posts last year. Can you help?

1 Like

I would say that it is related to your body. The json does not seem to be correctly formated but I am not an expert in testrigor. As far as I can tell other used the same and it worked

2 Likes

As far as I can tell other used the same and it worked

It’s not the same. Asuni is sending "true" as a string. It needs to be boolean - true. That’s why the API is unable to parse the body.

2 Likes

This is the documentation I referred to. [testRigor Language Support Documentation - testRigor AI-Based Automated Testing Tool]

In there , there is an example like this.

call api post "http://dummy.restapiexample.com/api/v1/create" with headers "Content-Type:application/json" and "Accept:application/json" and body "{\"name\":\"James\",\"salary\":\"123\",\"age\":\"32\"}" and get "$.data.name" and save it as "createdName"  and then check that http code is 200

That’s why I put quotation marks.I think that it’s a syntax pattern of testrigor.

1 Like

The response body I got is,

Do you have any idea of what I should do?

I’m sorry, from what tool did u send the request?

That is the exact same response body I got. I just copied out the important part for you in my previous post.

As I said, the problem is that the API is not recognising "true" as boolean because you’re sending it as a string.

2 Likes

It’s not a syntax pattern of testrigor, it’s a syntax pattern of a JSON. Strings have quotes, other data types don’t. Your example includes strings only.

2 Likes

It succeeded without quotation marks for gender and IsActive. Thank you very much for your support.

Glad it got resolved! Totally made myself a student on that platform right now hahahaa

1 Like

@restertest thank you for pinging me, this is definitely a testRigor test.

@asuni I recorded the video on where to find the API response to debug where the issue is: How to find API test response info in testRigor - YouTube

You can expand the test case, click on “More details” and then “Show extra info” to see the full response data.

StackOverflow is a good place to ask these questions and we are monitoring it constantly.