将 cURL 从 Swiftype API 转换为 Parse.httpRequest
Converting cURL from Swiftype API to Parse.httpRequest
我无法通过将其 cURL 转换为 Parse.httpRequest 方法来连接到 Swiftype API。我收到错误 400,信息 "document" object is missing 或 info to contact 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: 经过一些试验和错误后,以下代码进行了身份验证,但 returns "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);
}
});
但是当我尝试将对象提供给文档参数时,它给出了错误 "Can't form encode an Object"。
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 "contact support".
给我一个错误
终于成功了。问题? Header 需要是 JSON object
这是一个功能齐全的示例:
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);
}
});
我无法通过将其 cURL 转换为 Parse.httpRequest 方法来连接到 Swiftype API。我收到错误 400,信息 "document" object is missing 或 info to contact 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: 经过一些试验和错误后,以下代码进行了身份验证,但 returns "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);
}
});
但是当我尝试将对象提供给文档参数时,它给出了错误 "Can't form encode an Object"。
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 "contact support".
给我一个错误终于成功了。问题? Header 需要是 JSON object
这是一个功能齐全的示例:
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);
}
});