我無法通過將其cURL轉換爲Parse.httpRequest方法來連接到Swiftype API。我得到錯誤400與信息「文件」對象丟失或信息聯繫Swiftype。將cURL從Swiftype API轉換爲Parse.httpRequest
這裏是工作的捲曲:
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);
}
});
但是,當我嘗試提供對象到文檔參數,它給了我錯誤「可以' t形式編碼一個對象「。
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(文件),但它還是不給予我從Swiftype「聯繫支持」的錯誤工作。
最起碼,你不發出正確的內容類型,而你不發送在該AUTH_TOKEN數據。 – jcaron
@jcaron改變了這些,但沒有解決問題。我用其他調查結果更新了這個問題。 – Cyprian
爲什麼不像原始請求那樣將auth_token添加到正文中? – jcaron