+ Start a Discussion
.Net Api.Net Api 

Sample code for Http post

Hi ,

        Can ypu please send the sample code for http post in apex

sfdcfoxsfdcfox

The basic function looks like this:

 

 

String payLoad = 'some_data=value+1&some_more_data=value+2&etc_etc';
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.contoso.com/');
req.setMethod('POST');
req.setBody(payLoad);
HttpResponse res = h.send(req);

This is basically outlined in the relevant section in the Apex Code Developer's Guide, but isn't specifically spelled out that way.

 

.Net Api.Net Api

 HttpRequest req = new HttpRequest();
    Http http = new Http();
    HttpResponse resp = new HttpResponse();
    String Uname='raviaruva';
    String pwd='raviaruva';
    
        
       
    req.setEndpoint('http://xyz/BMEapp/API.asmx/DataSink');
    req.setBody('username='+EncodingUtil.urlEncode(Uname, 'UTF-8')
                +'&password='+EncodingUtil.urlEncode(pwd, 'UTF-8')
                    +'&info='+EncodingUtil.urlEncode(data, 'UTF-8'));                   
                     

     req.setMethod('POST');
     req.setHeader('content-type', 'text');
     req.setHeader('Content-Length','10240');
     
        printUrl = req.toString();
        req.setCompressed(true);                
     // add the endpoint to the request
   try {
            resp = http.send(req);
        }  catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(resp.toString());
            
        }

 

Its giving 500 Error

 

What is the problem

WhiteWhite

 HttpRequest req = new HttpRequest();
    Http http = new Http();
    HttpResponse resp = new HttpResponse();
    String Uname='raviaruva';
    String pwd='raviaruva';
    
        
       
    req.setEndpoint('http://xyz/BMEapp/API.asmx/DataSink');
    req.setBody('username='+EncodingUtil.urlEncode(Uname, 'UTF-8')
                +'&password='+EncodingUtil.urlEncode(pwd, 'UTF-8')
                    +'&info='+EncodingUtil.urlEncode(data, 'UTF-8'));                   
                     

     req.setMethod('POST');
     req.setHeader('content-type', 'text');
     req.setHeader('Content-Length','10240');
     
        printUrl = req.toString();
        req.setCompressed(true);                
     // add the endpoint to the request
   try {
            resp = http.send(req);
        }  catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(resp.toString());
            
        }

 

Its giving 500 Error

 

What is the problem

sfdcfoxsfdcfox

A return value of 500 means that your server encountered an unusual condition (a script or environment error). This means, most likely, that your data is incorrect. I suspect that the 500 error occurred because of a lack of data; your POST body is supposed to be 10,240 bytes, and you are sending far less than that, which is probably why the connection fails.

.Net Api.Net Api

Hello,

            Can you please send me the working sample code .

 

Regards.

Praveen Gaddam

.Net Api.Net Api

Hi All,

            This is the solution

 

                 String strUsername='praveen';
        String strPwd='praveen';
        Http m_http = new Http();
        HttpRequest req = new HttpRequest();
        
        String content = 'username='+EncodingUtil.urlEncode(strUsername, 'UTF-8')+'&password='+EncodingUtil.urlEncode(strPwd, 'UTF-8')+'&listname='+EncodingUtil.urlEncode('PraveenKumarG', 'UTF-8');
        
        req.setEndpoint('http://xyz/BMEapp/API.asmx/listCreate');
        req.setHeader('Content-Type','application/x-www-form-urlencoded');        
        req.setMethod('POST');
        req.setBody(content);
        
        httpResponse response = m_http.send(req);

 

 

 

Regards,

Praveen Gaddam

タン ビン チャンタン ビン チャン
   HttpRequest req = new HttpRequest();
        req.setEndpoint(https://alivecoupon.com);
        req.setMethod('GET');
        
        Http http = new Http();
        HttpResponse res = http.send(req);
        String body = res.getBody();
        
        system.debug('Our response: ' + body);
Parikhit SarkarParikhit Sarkar
The following apex class example will allow you to set parameters in the query string for a post request -

    @RestResource(urlmapping = '/sendComment/*')

    global without sharing class postComment {
  
    @HttpPost
    global static void postComment(){
               
        //create parameters 

        string commentTitle = RestContext.request.params.get('commentTitle');
        string textBody = RestContext.request.params.get('textBody');       
        
        //equate the parameters with the respective fields of the new record

        Comment__c thisComment = new Comment__c(
            Title__c = commentTitle,
            TextBody__c = textBody, 
            
        );
        
        insert thisComment; 
        
        
        RestContext.response.responseBody = blob.valueOf('[{"Comment Id": 
        '+JSON.serialize(thisComment.Id)+', "Message" : "Comment submitted 
        successfully"}]');
        }
    }


The URL for the above API class will look like - 

/services/apexrest/sendComment?commentTitle=Sample title&textBody=This is a comment