function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Test Dev 81Test Dev 81 

Upload text file to server using apex code

Hi Everyone, 
I am trying to upload a text file to Docufree for too long but I am not able to, getting so many issues
here's the REST API link: I am taking an example from
http://https://saas.docufree.com/dfwebservice/docs/guide/upload.html#payload-format
 
public static HTTPResponse uploadFile(Attachment file) {
        Rg_FileJSon jsonObj = new Rg_FileJSon();
        List<Rg_FileJSon.Files> Files = new List<Rg_FileJSon.Files>();
        Rg_FileJSon.Files jsonFile = new Rg_FileJSon.Files();
        jsonFile.name = file.Name;
        jsonFile.doctype = file.ContentType;
        jsonFile.data = String.valueOf(file.Body);
        Rg_FileJSon.Index ind = new Rg_FileJSon.Index();
        ind.name = 'index a';
        ind.typeId = 0;
        ind.value = '1';
        jsonFile.Index = new List<Rg_FileJSon.Index>{ind};
        Files.add(jsonFile);
        jsonObj.files = Files;
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type','application/json');
        req.setMethod('POST');
        req.setEndpoint('https://saas.docufree.com/dfwebservice/v3/files/capture/folder?targetId=3486');
        req.setBody(body);
        req.setTimeout(60000);
        Rg_FileUploadService obj = new Rg_FileUploadService();
        string accessToken = obj.genAccessToken();
        req.setHeader('Authorization','Bearer ' + accessToken);
        System.debug('request : '+req.getBody());
        Http http = new Http();
        return http.send(req);
    }
 
//JSON Class
public class Rg_FileJSon {
    public class Index {
        public Integer typeId;
        public String name;
        public String value;
    }
    public class Files {
        public String name;
        public String data;
        public String doctype;
        public List<Index> index;
    }
    public List<Files> files;
    public Integer bindFileId;
    public static Rg_FileJSon parse(String json) {
        return (Rg_FileJSon) System.JSON.deserialize(json, Rg_FileJSon.class);
    }
}

i Don't know what I am doing wrong here, getting 500 internal server error
Guy please help me to solve this issue
ShivankurShivankur (Salesforce Developers) 
Hi Test Dev 81,

First step for debugging this would be testing this endpoint with help of Docufree API reference site itself. If it succeeds there then it can be further debugged in Apex.

You can test your endpoint by authorizing into below swagger link from Docufree:
https://saas.docufree.com/dfwebservice/docs/guide/swagger.html

Authorization can be done with help of available scopes as openid,df_api.

Once authorized, you can provide the target and targetId values to the POST request /v3/files/capture/{target} and try executing it.

Also,as you mentioned you get 500 internal server error, can you try putting debug logs in Salesforce org to see any further hints.Please attach a screenshot on this thread for reference too.

Thanks.