Uploading file via REST API/Python

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

 

 

Answers

  • Hi,

     

    How did you test that the upload did not work ?

  • 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.

  • are you checking with the same user ?
    you should print the answer from the endpoint, would help
  • 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

     

  • Hi,

    try first the upload part 

    requests.put(url_file,headers=build_headers(user, 'application/octet-stream'), data=file_data)

    and check the file in the Data Sources

    ABerenguela_0-1589882097919.png

    in this "storage" is where Anaplan keep the files, the data in the files will be different for each user, so you need to login with the API user to do this validation, this is @nathan_rudman recommendation.

    Then try to run the Import manually in Anaplan to see if there is some issue (the import takes the data from the uploaded file and load it into a module)

    Regards