C++Rest SDK Passing a long data type into request body results in 400 Bad Request

225 views Asked by At

My Swagger document defines a long data type expected to be sent in request like:

    size:
      type: integer
      format: int64
      description: 'Some size'

I create request using following code in C++:

web::json::value postData = web::json::value::object();

If I pass a hardcoded value (i.e 50 or so) in the body as below, I get 200 OK response, so that is good

postData[L"size"] = web::json::value::number(50);

However, if I pass this value as a long or int64_t data type variable like below, I get 400 Bad Request

long size = 50;   //or int64_t, it results with same error
postData[L"size"] = web::json::value::number(size);
1

There are 1 answers

0
pixel On BEST ANSWER

The problem was that I use MockLabs to create mock calls. So, in there I had POST request body defined to be like:

{ "size":1 }

So, if my incoming POST request had a value different than 1 (i.e 50), I would get 400 Bad Request; otherwise, I would get 200 OK. If you keep this POST request body, then you can only send size=1, all other will return 400 Bad Request.

Since I was hardcoding 1, I was getting 200 OK But when I used a variable in my code (which is different than 1), I would get 400 Bad Request.

The solution: Simply remove POST request body definition in your mock server (I use MockLabs) and leave it blank. Then it will accept any POST request with body (i.e { "size":1 } or { "size":450 }, or { "size":8 }, or whatever other integer you want) and will response with 200 OK.