我无法通过将其 cURL 转换为 Parse.httpRequest 方法来连接到 Swiftype API。我收到错误 400 信息“文档”对象丢失或联系 Swiftype 的信息。
这是有效的 cURL:
curl -XPOST 'https://api.swiftype.com/api/v1/engines/xxx/document_types/xxx/documents.json' \
-H 'Content-Type: application/json' \
-d '{
"auth_token":"xxx",
"document": {
"external_id": "1",
"fields": [
{"name": "title", "value": "The Great Gatsby", "type": "string"},
{"name": "author", "value": "F. Scott Fitzgerald", "type": "string"},
{"name": "genre", "value": "fiction", "type": "enum"}
]
}
}'
注意:我输入了 xxx 来隐藏我正在使用的实际密钥。
Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://xxx:@api.swiftype.com/api/v1/engines/xxx/document_types/xxx/documents.json',
header: 'Content-Type: application/x-www-form-urlencoded',
body:{'document': '{"external_id": "1","fields": []}'},
success: function(httpResponse) {
console.log(httpResponse.text);
response.success(httpResponse.text);
},
error: function(httpResponse) {
console.error('Request failed with response ' + httpResponse.text);
response.error(httpResponse.text);
}
});
更新 1:经过一些试验和错误后,以下代码进行了身份验证,但返回缺少“文档”参数
Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.swiftype.com/api/v1/engines/xxx/document_types/xxx/documents.json',
headers: 'content-type: application/json',
params: {auth_token:'xxxx'},
body: '{"document":{}}',
success: function(httpResponse) {
console.log(httpResponse.text);
response.success(httpResponse.text);
},
error: function(httpResponse) {
console.error('Request failed with response ' + httpResponse.text);
response.error(httpResponse.text);
}
});
更新 2:这验证
Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.swiftype.com/api/v1/engines/xxx/document_types/xxx/documents.json',
headers: 'content-type: application/json',
body:{auth_token:'xxx',
document: '{}'},
success: function(httpResponse) {
console.log(httpResponse.text);
response.success(httpResponse.text);
},
error: function(httpResponse) {
console.error('Request failed with response ' + httpResponse.text);
response.error(httpResponse.text);
}
});
但是当我尝试将对象提供给文档参数时,它给了我错误“无法对对象进行编码”。
Parse.Cloud.httpRequest({
method: 'POST',
url: 'https://api.swiftype.com/api/v1/engines/xxx/document_types/xxx/documents.json',
headers: 'content-type: application/json',
body:{auth_token:'xxx',
document: {'external_id': '1'}},
success: function(httpResponse) {
console.log(httpResponse.text);
response.success(httpResponse.text);
},
error: function(httpResponse) {
console.error('Request failed with response ' + httpResponse.text);
response.error(httpResponse.text);
}
});
我尝试了 JSON.stringify(document) ,但这也不起作用,因为 Swiftype“联系支持”给我一个错误。