【发布时间】:2017-05-17 14:03:04
【问题描述】:
我正在尝试发出一个简单的 API 请求,但惨遭失败。
文档说这是提出请求的方法:
POST https://api.yotpo.com/oauth/token
--------------------------------------
{
"client_id": "### Your client_id ###",
"client_secret": "### Your client_secret ###",
"grant_type": "client_credentials"
}
我尝试通过这样的 HTML 表单提交它只是为了测试:
<head>
<meta http-equiv="content-type" content="application/json; charset=UTF-8">
</head>
<?php
$json ='{"client_id": "creds",
"client_secret": "secret",
"grant_type": "client_credentials"}';
print_r($_POST);
?>
<form action="https://api.yotpo.com/oauth/token" enctype='application/json' method="POST">
<input type="hidden" name="post_data" <?php echo 'value="'.htmlspecialchars($json).'"'; ?> />
<input type="submit">
</form>
有人告诉我内容类型必须是我设置的application/json。
我通过 PHP 测试文件在我的服务器上执行此操作,但实际上我愿意以任何可能的方式进行这项工作,然后从那里解决。
当我从当前的 HTML 表单提交时,页面重定向到 yotpo 页面,并且 JSON 数据显示为空白:
{"status":{"message":"Couldn't find Account with app_key = ","code":404,"error_type":"Exceptions::RecordNotFound"}}
提交此 API 身份验证的最简单方法是什么?
【问题讨论】:
-
我被告知内容类型需要是我设置的 application/json。 不。你没有设置。
enctype属性不能使 Content-type 为application/json。它只能设置为application/json不属于的MIME 类型之一。换句话说,您不能将 JSON 作为form数据发送。您必须使用 AJAX 来发送此类数据。
标签: javascript html json http