gpt4 book ai didi

javascript - 将 cURL 从 Swiftype API 转换为 Parse.httpRequest

转载 作者:行者123 更新时间:2023-11-29 15:31:43 29 4
gpt4 key购买 nike

我无法通过将其 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 是为了隐藏我正在使用的实际 key 。

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“联系支持”给我一个错误。

最佳答案

终于搞定了。问题? 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);
}
});

关于javascript - 将 cURL 从 Swiftype API 转换为 Parse.httpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34287490/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com