php-mod/curl

Cannot send boolean value

matsubo opened this issue · 4 comments

I passed following data to post method.

array(4) {
  ["amount"]=>
  int(900)
  ["currency"]=>
  string(3) "JPY"
  ["card"]=>
  string(28) "tok_SMJhkoibbHr9CYsv2wwcZ1Ik"
  ["capture"]=>
  bool(false)
}

However the value of capture is changed from false to 0.

string(67) "amount=900&currency=JPY&card=tok_SMJhkoibbHr9CYsv2wwcZ1Ik&capture=0"

This is caused http_build_query inside of post method.
https://github.com/php-mod/curl/blob/master/src/Curl/Curl.php#L60

This case of scenario is missing in test cases.

Thank you @matsubo for the bug reporting. But as I know we can't post boolean data. All data is treated as strings. So boolean data will be converted to strings before to send them via POST. http_build_query convert false to 0, true to 1. If we suppose another function which converts them to "true" and "false", how can guest that were boolean and not a simple strings? Do you have an idea?

What you can do is something like that:

$capture = (bool) $_POST['capture'];
If your application is waiting for strings: "true" and "false", then you change your array to:
[
"amount" => 900
"currency" => "JPY"
"card" => "tok_SMJhkoibbHr9CYsv2wwcZ1Ik"
"capture" => $capture ? "true" : "false"
]

You can also use filters: http://php.net/manual/en/function.filter-var.php

So this is not a bug. This is an HTTP behavior.

Yes. I noticed there is no variable type on HTTP. I was confused with JSON response data.
Thank you for your kindly reply.