1

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"
}
Alex Polkhovsky
  • 3,340
  • 5
  • 29
  • 37
  • Maybe you could try curl, to isolate whether the problem is the payload you're sending or the request format. If it works in curl, then I'd guess it's something with your HTTP request. For example, maybe your content-type is `application/json` (it should be multi-part form - https://docs.truevault.com/overview#api-requests). If you can share the exact request you're making (make one with a garbled schema id if you want) that would help. – andrewmitchell Nov 13 '17 at 19:05
  • I did. Same result. Should’ve mentioned it in the question. – Alex Polkhovsky Nov 13 '17 at 19:14
  • I added the `curl` command. – Alex Polkhovsky Nov 15 '17 at 22:58

2 Answers2

0

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.

andrewmitchell
  • 1,559
  • 13
  • 15
  • `curl -d "search_option=eyJzY2hlbWFfaWQiOiJlOWVxxxxxxxxxxN2EtOGZiOC1jNzY0OTU3ZmMwZGUiLCJmaWx0ZXIiOnsibGFzdF9uYW1lIjp7InR5cUiOiJ3aWxkY2FyZCIsInZhbHVlIjoiRxxxxxxxxxxx" -X POST -H "Authorization: Basic MDkwMDRxxxxxxxxxxxxxWYtMjVkODFiMWE5MmE0Og==" -H "Content-Type: multipart/form-data" https://api.truevault.com/v1/vaults/xxxxxxxx-15e3-442e-aa6f-xxxxxxxx/search` produces the same error. – Alex Polkhovsky Nov 28 '17 at 02:12
  • At this point, I think your problem is likely the search_option value itself. Perhaps start simple with an example from the docs, see if a search option published here works: https://docs.truevault.com/documentsearch#defining-search-options. If you can get it working that way, then revisit your search_option and the documentation to see what's wrong. – andrewmitchell Nov 30 '17 at 16:30
  • I AM using the simplest example from the documentation! – Alex Polkhovsky Dec 01 '17 at 15:40
  • `{ schema_id: 'xxxxxxxx-8b39-427a-8fb8-c764957fd9c6', filter: { last_name: { type: 'not', value: 'Smith' } } }` – Alex Polkhovsky Dec 01 '17 at 15:41
  • If that is what you are literally base64 encoding and submitting, then you problem is that what you quoted above is not JSON. You must base64 a valid JSON string, such as `{"schema_id": "123", "filter": {"last_name": {"type": "not", "value": "Smith"} } }. – andrewmitchell Dec 02 '17 at 23:14
  • That JSON is being converted into a string using JSON.stringify, and then converted to a base64 string per the documentation. I detailed the process in the original post. I think at this point it would be more productive for you to provide a working example. Either the documentation is wrong or the API is buggy. We're reduced to hacking/guessing what the API is expecting and it's a waste of everybody's time. Thanks. – Alex Polkhovsky Dec 04 '17 at 01:06
0

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 ) {
        //
    }
});
DACrosby
  • 11,116
  • 3
  • 39
  • 51