1

The code below is part of the Python Quickbase module which has not been updated in quite a while. The help text for one of the function shown below is not clear on how to pass the parameters to upload a file (the value of which is actually base64 encoded).

def add_record(self, fields, named=False, database=None, ignore_error=True, uploads=None):
    """Add new record. "fields" is a dict of name:value pairs
    (if named is True) or fid:value pairs (if named is False). Return the new records RID
    """
    request = {}
    if ignore_error:
        request['ignoreError'] = '1'
    attr = 'name' if named else 'fid'
    request['field'] = []
    for field, value in fields.iteritems():
        request_field = ({attr: to_xml_name(field) if named else field}, value)
        request['field'].append(request_field)
    if uploads:
        for upload in uploads:
            request_field = (
                {attr: (to_xml_name(upload['field']) if named else upload['field']),
                 'filename': upload['filename']}, upload['value'])
            request['field'].append(request_field)

    response = self.request('AddRecord', database or self.database, request, required=['rid'])
    return int(response['rid'])

Can someone help me in how I should pass the parameters to add a record.

martineau
  • 119,623
  • 25
  • 170
  • 301
speedchase
  • 41
  • 1
  • 7

1 Answers1

1

Based on the definition you provided, it appears that you you need to pass an array of dictionaries that each provide the field name/id, filename, and the base64 encoding of the file for the uploads parameter. So, if I had a table where I record the name of a color to the field named "color" with the field id of 19 and a sample image to the field named "sample image" with the field id of 21, I believe my method call would be something like:

my_color_file = #base64 encoding of your file
my_fields = {'19': 'Seafoam Green'}
my_uploads = [{'field': 21, 'filename':'seafoam_green.png', 'value': my_color_file}]
client.add_record(fields=my_fields, uploads=my_uploads)

Or, if you're using field names:

my_color_file = #base64 encoding of your file
my_fields = {'color': 'Seafoam Green'}
my_uploads = [{'field': 'sample_image', 'filename':'seafoam_green.png', 'value': my_color_file}]
client.add_record(fields=my_fields, named=True, uploads=my_uploads)

client is just the object you instantiated earlier using whatever constructor this module has.

Nathan Hawe
  • 281
  • 3
  • 5
  • My understanding is that the uploads parameter needs to have 3 values - field_Id/field name, filename and base64 encoded file to be passed in the XML as shown here [link](https://www.quickbase.com/api-guide/add_record.html#base64-encoded_file_attachment) – speedchase Jul 24 '16 at 19:24
  • You are correct. I updated my answer to reflect that the method expects an array of dictionaries instead of a single dictionary and that the dictionary has three key/value pairs instead of the one I originally wrote. – Nathan Hawe Jul 25 '16 at 18:32
  • Perfect ! It worked like a charm. Now I can just iterate this API request over the scanned image files on my local computer for upload to Quickbase. – speedchase Jul 25 '16 at 19:57