Converting cURL from Swiftype API to Parse.httpRequest

200 Views Asked by At

I can't connect to the Swiftype API by converting their cURL to Parse.httpRequest method. I get either error 400 with info "document" object is missing or info to contact Swiftype.

Here is the cURL that works:

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"}
    ]
  }
}'

Note: I entered xxx to hide the actual key I am using.

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);
        }
    });

Update 1: After some trials and errors the following code authenticates but returns that the "document" parameter is missing

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);
        }
    });

Update 2: This validates

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);
        }
    });

But when I try to supply object into the document parameter it gives me the error "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);
        }
    });

I tried JSON.stringify(document) but that didn't work either by giving me an error from Swiftype "contact support".

1

There are 1 best solutions below

4
On BEST ANSWER

Finally got it to work. The problem? Header needs to be a JSON object

Here is a fully functioning example:

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);
        }
    });