Uploading file via REST API/Python

Options
johngervacio
johngervacio Member, ALL USERS Posts: 3 Not applicable

Hi, I am working on an item that uploads .csv file into Anaplan. I'm not sure why the upload doesn't work even if the response of API is successful. I've tried using some codes posted here but seems the upload does not work (download, import seems okay)

 

I'm using basic authentication and 1.3 version of the API.

 

Here is a snippet of the code

 

    user = AnaplanUtils.create_auth_session()
    file_id = '123'
    file_name = 'test.csv'
    url_file = api_url + 'files/'
    url_complete = url_file + "/complete"

    file_metadata_start = {
        "id": file_id,
        "chunkCount": -1
    }
    file_metadata_complete = {
        "id": file_id,
        "chunkCount": -1
    }

    start_upload_post = requests.post(url_file, headers=build_headers(user, 'application/json')
                                      , json=file_metadata_start)

    file_data = open(file_name, "r").read().encode('utf-8')

    if start_upload_post.ok:
        file_upload = requests.put(url_file,
                                   headers=build_headers(user, 'application/octet-stream'), data=file_data)
        if file_upload.ok:
            complete_upload = requests.post(url_complete, headers=build_headers(user, 'application/json')
                                            , json=file_metadata_complete)
            if complete_upload.ok:
                print('success')
            else:
                print('There was an issue with your file upload: '
                      + str(complete_upload.status_code))
        else:
            print('There was an issue with your file upload: '
                  + str(file_upload.status_code))

 

 

Comments

  • johngervacio
    johngervacio Member, ALL USERS Posts: 3 Not applicable

    Hi Nathan,

     

    Because the data on the file is not being reflected on Anaplan (I always check on UI after executing the code but no change is happening).

    I'm not sure if I'm doing it correctly on my code.

  • johngervacio
    johngervacio Member, ALL USERS Posts: 3 Not applicable

    The target table is already created(created by another user) but the import is created using my account and the credentials in the API is the same.

     

    I tried to print the text response of the complete API, seems it returned the details of the import data source which is expected and http response is 200