我无法通过将其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:经过一些试验和错误之后,以下代码进行了身份验证,但返回缺少“document”参数的信息
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);
}
});
但是,当我尝试将对象提供给document参数时,它给我错误“无法对对象进行形式编码”。
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“联系支持”一个错误也没有起作用。
最佳答案
终于让它工作了。问题? header 必须是JSON对象
这是一个功能齐全的示例:
var bodyData = {
auth_token:"the_token",
document: {
external_id: "1",
fields: [{name: "title", value: "The Great Gatsby", type: "string"}]
}
};
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: bodyData,
success: function(httpResponse) {
console.log(httpResponse.text);
response.success();
},
error: function(httpResponse) {
console.error('Request failed with response ' + httpResponse.text);
response.error(httpResponse.text);
}
});