Help needed with Rest API file upload (using R if possible)

Hello everyone, I am trying to upload a file onto anaplan from R and haven't been successful for quite a bit of time. 

 

I will list down the steps I have taken and it would be greatly appreciated if anyone can help

 

1. Created a file in anaplan and set the option to "Admins Only". The file name is driver_coeff_template.csv

sunilrinald_0-1618919736740.png

 

2. Created an import action that uses this file to update data in a module

 

sunilrinald_1-1618919862036.pngsunilrinald_2-1618919878134.png

 

3. Here are the actions that i run through R. I will attach the script that has i built using the RestAPI documentation. The first one is authentication, i am able to login and generate a token. 

 

4. Using GET to fetch file details

5. Using POST to set chunk count to -1. (The file that needs to be uploaded is very small usually ~100 KB)

6. Using PUT to upload file. The upload file has data as shown below in the screenshot. Also this step always seems to return NULL as response in R. 

sunilrinald_3-1618920597400.png

 

 

 

 

7. Using POST to mark upload as complete. Once this is done, i checked out the file in anaplan 

sunilrinald_4-1618920721484.pngsunilrinald_5-1618920820076.png

It seems that part of the file has been loaded which is the first column "Estimate" but the row positions are changed and i am missing the other columns. 

 

Would really help any help on this and as mentioned before, i have attached the R code as well in a text file. The code simply uses the URLs for the api and has functions for headers as well as body that has to be sent in case anyone find it hard to understand. I have looked at the discussion a few times https://community.anaplan.com/t5/Anaplan-Platform/Uploading-file-Via-REST-API-and-Triggering-Imports/m-p/51791#M7821 as well as the Rest API videos in https://community.anaplan.com/t5/How-To/RESTful-API-How-To/ta-p/33579 but haven't been able to get the upload right. 

 

 

Best Answer

  • ben_speight
    Answer ✓

    I've never seen the R language used with the Anaplan API, so this is uncharted terrain for me too. The problem is caused by the use of the upload_file function to prepare the PUT request body. The only way to solve it I can think of is to read the file content into a raw vector and pass that to the PUT function instead, along with encode="raw".

Answers

  • It looks like R (and/or the underlying libcurl) has uploaded the file as a multipart MIME-encoded form body the same way a browser upload would, which is not the same as a raw binary upload, like if you invoked curl directly using

    -X PUT --data-binary @file

    I can't see any way to alter this behaviour, so you might have more success scripting the curl command directly.

  • hi ben, @ben_speight  could you please elaborate a bit more on this, finding it hard to understand as i am not very well versed with APIs 

  • Let me try this @ben_speight  and will let you know what happens

  • thank you so much @ben_speight , this worked out perfectly for a single chunk file. Been at this for a while so really appreciate the help.