根据API文档,curl请求应为:
$data =array ('currentPage' => 1,
'itemsPerPage' => 10
);
$hash = sha1(http_build_query($data) . sha1('password'));
$requestData = array(
'code' => ‘usercode',
'username' => 'username',
'data' => $data,
'hash' => $hash);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://mktp-stage.emag.ro/api-3/vat/read');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
$result = curl_exec($ch);
我的应用脚本为:
var headers = {
"Content-Type": "application/json",
"Authorization": "Basic "+ Utilities.base64Encode(user+":"+password)
};
var options = {
"method" : "POST",
"headers" : headers,
"payload" : {
"modifiedAfter" : "2018-12-31 01:00:00",
"status" : 4
}
};
var response = UrlFetchApp.fetch(url,options);
我得到结果,但未通过“ modifiedAfter”过滤:“ 2018-12-31 01:00:00”, “状态”:4。 我在做什么错了?
更新:我使用了Dimu Designs示例,但结果仍未过滤。 他们(API所有者)说,我应该像curl请求中那样以数组形式发送过滤器,因为API不接受json请求,而仅返回json格式:
$data =
array (
'currentPage' => 1,
'itemsPerPage' => 10
);
$hash = sha1(http_build_query($data) . sha1('password'));
$requestData = array(
'code' => ‘usercode',
'username' => 'username',
'data' => $data,
'hash' => $hash);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
谢谢!
答案 0 :(得分:0)
您的代码有两个问题。
首先,Apps Script有一个奇怪的警告,其中内容类型标头设置为options
对象的属性,而不是headers
对象的属性。
第二,您需要JSON.stringify
您的有效载荷对象。
因此您的代码应为:
var headers = {
"Authorization":"Basic "+ Utilities.base64Encode(user+":"+password)
};
var payload = JSON.Stringify({
"modifiedAfter":"2018-12-31 01:00:00",
"status":4
});
var options = {
"method":"POST",
"contentType":"application/json",
"headers":headers,
"payload":payload
};