php - 为什么我会收到“BAD_REQUEST 无法处理的 JSON”?
问题描述
我正在尝试调用 REST-API,但它只返回“BAD_REQUEST Unprocessable JSON”。
我已经尝试将输入重写为我正在遵循的示例,但它只是给了我同样的错误。
这是我用于发送请求的代码:
function callAPI($method, $url, $data)
{
$curl = curl_init();
$username = 'PK10008_e8f77aebdb0a';
$password = 'rwBmthNFK8JTbIL7';
switch($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
}
//Options
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result)
{
die("Connection Failure");
}
curl_close($curl);
return $result;
}
这是我用来创建和发送信息的代码:
$data_array = array(
'purchase_country'=> 'SE',
'purchase_currency'=> 'SEK',
'locale'=> 'sv-SE',
'order_amount'=> 10,
'order_tax_amount'=> 0,
'order_lines' => array(
'type'=> 'physical',
'reference'=> '19-402',
'name'=> 'Test',
'quantity'=> 1,
'unit_price'=> 10,
'tax_rate'=> 0,
'total_amount'=> 10,
'total_discount_amount'=> 0,
'total_tax_amount'=> 0
)
);
$make_call = callAPI('POST', 'https://api.playground.klarna.com/payments/v1/sessions', json_encode($data_array));
$response = json_decode($make_call, true);
这是一个例子:
POST /payments/v1/sessions
Authorization: Basic pwhcueUff0MmwLShJiBE9JHA==
Content-Type: application/json
{
"purchase_country": "SE",
"purchase_currency": "SEK",
"locale": "sv-SE",
"order_amount": 10,
"order_tax_amount": 0,
"order_lines": [{
"type": "physical",
"reference": "19-402",
"name": "Battery Power Pack",
"quantity": 1,
"unit_price": 10,
"tax_rate": 0,
"total_amount": 10,
"total_discount_amount": 0,
"total_tax_amount": 0
}]
}
我得到的结果是:'BAD_REQUEST Unprocessable JSON 1734c472-c9cb-4022-b469-a8231c7abeec'
但我应该得到这样的东西:
HTTP/1.1 200 OK
Content-Type: application/json
{
"session_id": "068df369-13a7-4d47-a564-62f8408bb760",
"client_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjAwMDAwMDAwMDAtMDAwMDAtMDAwMC0wMDAwMDAwMC0wMDAwIiwidXJsIjoiaHR0cHM6Ly9jcmVkaXQtZXUua2xhcm5hLmNvbSJ9.A_rHWMSXQN2NRNGYTREBTkGwYwtm-sulkSDMvlJL87M",
"payment_method_categories": [{
"identifier": "pay_later"
"name" : "Pay later.",
"asset_urls" : {
"descriptive" : "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_us/pay_later/descriptive/pink.svg",
"standard" : "https://cdn.klarna.com/1.0/shared/image/generic/badge/en_us/pay_later/standard/pink.svg"
}
}]
}
解决方案
我遇到的问题是 json_encode 如何创建编码数组。而不是这个:"example":[{
"ex": "ex"
}]
它这样做了:
"example":{
"ex": "ex"
}
这反过来又使最终 API 无法读取 json
推荐阅读
- reactjs - react-navigation v5 中不显示图标
- c++ - CPU瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- javascript - 导航返回时无法在单选按钮之间导航
- python - 使用 Dataframe 中的列表理解检测和更改
- c# - 使用 WebClient (.Net 4.0) 在 Web API 上执行 PATCH
- python - 使用 selenium Python 库单击下拉菜单中的项目
- installation - 我现在是否应该将这些文件包含在我的 [InstallDelete] 部分中,因为它们不再需要?
- angular - Webcomponentsjs polyfill 在 IE11 中不起作用(没有足够的堆栈空间)
- c# - Mac 上的 Visual Studio 2019 在 asp.net 核心项目运行时崩溃
- spring - Spring MVC - 无法登录