3

If we are using json in post Method by clicking Raw i have given the Request body as like

{
  "name" : "Wingo Ducks",
  "phone" : "707-555-1234",
  "website" : "www.wingo.ca.us"
}

For this body i am getting through apex class like this

@HttpPost
global static String doPost(String name, String phone, String website) {
    Account account = new Account();
        account.Name = name;
        account.phone = phone;
        account.website = website;
    insert account;
return account.Id;

If I am given the Request body like uploading the File by clicking form as account.txt which have this file in my desktop

how to write a apex code for post method to get through the file in apex class

please can anyone guide Me for the Answer.........!

1 Answer 1

1

@HttpPost

global static string dopost(){
    RestRequest req = RestContext.request;
    system.debug(req);
    system.debug(req.requestbody);
    Blob body = req.requestBody;
    system.debug(body);

    string body1 =body.tostring();
    system.debug(body1);
    Blob body2=Blob.toPDF(body1);
    system.debug(body2);

    string[] accountDataLines = new String[]{};
    accountDataLines = body1.split('\n');
    system.debug(accountDataLines[6]);
    system.debug(accountDataLines[8]);
    system.debug(accountDataLines[10]);
    list<string> inputvalues =accountDataLines[6].split(':'); 
    system.debug(inputvalues[1]); 
    list<string> inputvalues1 =accountDataLines[8].split(':'); 
    system.debug(inputvalues1[1]);
    list<string> inputvalues2 =accountDataLines[10].split(':'); 
    system.debug(inputvalues2[1]); 
    account cc = new account();
    cc.Name= inputvalues[1];
    cc.phone=inputvalues1[1]; 
    cc.Website=inputvalues2[1];
    insert cc;

     return cc.id;
 }

Try this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .