Searching TrueVault always returns INVALID_SEARCH_QUERY

201 Views Asked by At

Dead simple search query only using required fields:

{ schema_id: 'xxxxxxxx-8b39-427a-8fb8-c764957fd9c6',
filter: { last_name: { type: 'not', value: 'Smith' } } }

POSTing to https://api.truevault.com/v1/vaults/xxxxxxxx-15e3-442e-aa6f-xxxxxxxx/search

When POSTing, the POST call options look like this:

{ 
data: { search_option: 'xxxx base64 encoded JSON.stringify of the above xxxxxxx' },
headers: { Authorization: 'Basic xxx base64 encoded API KEY xxx' } 
}

Authorization is working. Result:

{ Error: failed [400] { "error": { "code": "SEARCH.INVALID_SEARCH_QUERY", "message": "Invalid search_option.", "type": "invalid_request_error" },
"result": "error", "transaction_id": "9ad83046-1906-406c-87ab-963b904857c4" }

curl command for the same search query:

curl -d "{ search_option: 'eyJzY2hlbWFfaWQiOiJlOWVmYmE0NC04YjMwLTQyN2EtOGZiOC1jNzY0OTU3ZmMwZGUiLCJmaWx0ZXIiOnsibGFzdF9uYW1lIjp7InR5cGUiOiJ3aWxkY2FyZCIsInZhbHVlIjoiRnJhbnptZWllcioifX19' }" 
-X POST 
-H "Content-Type: application/json" 
-H "Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==" 
https://api.truevault.com/v1/vaults/xxxxxxxx-15e3-442e-aa6f-4xxxxxxxxxx/search

Same error:

{
    "error": {
        "code": "SEARCH.INVALID_SEARCH_QUERY",
        "message": "Invalid search_option.",
        "type": "invalid_request_error"
    },
    "result": "error",
    "transaction_id": "b5a51185-264f-4765-a1b8-6ae9e491aa39"
}
2

There are 2 best solutions below

6
andrewmitchell On

Alex,

I don't think your curl command is formatted the way TrueVault expects. This may also be your problem in your http library. The curl command I'd expect is:

curl https://api.truevault.com/v1/vaults/xyz/search \
 -XPOST \
 -H "Content..."
 -H "Auth..."
 -d "search_option=eyJ...."

That is, the data payload is form-encoded (param1=value1&param2=value2) not JSON.

It looks like the docs for search were missing a sample curl, but you can see a similar curl for document create listed in the docs.

0
DACrosby On

I ran into I believe the same issue in my juery ajax request. For me, it was that I was sending my request with header{'Content-Type':'application/json'}, but it needs to be form-data.

Working example:

var api_url = 'https://api.truevault.com/v1/vaults/' + vault_id + '/search';
var data = {
        'schema_id'    : schema_id,
        'page'         : 1,
        'per_page'     : 50,
        'filter_type'  : 'and',
        'full_document' : false,
        "sort": [
            {
                "SomeField":"asc"
            }
        ],
        'filter':{
            'SomeField':{
                'type':'eq',
                'value': somevalue
            }
        }
    };
data = btoa( JSON.stringify( data )  );

$.ajax({
    type     : 'POST',
    url      : api_url,
    dataType : 'json',
    processData : false,
    headers : {
        'Authorization':'Basic '+ btoa( auth_key+":" ),
    },
    mimeType : "multipart/form-data",
    data : 'search_option='+ data,
    error: function( response ) {
        //
    },
    success: function( response ) {
        //
    }
});