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
DJAISHDJAISH 

HttpRequest question

I'm having some difficulty wrapping my head around the HttpRequest function, was hoping someone could push me along.

 

I'm trying to forward some key information to my web database on every save of a single custom object. My understanding was that using HttpRequest would be similar to sending a form to the page and I would be able to parse the array values there.

 

My difficulty is understanding how to tell the HttpRequest which Object I want it to function off of and more importantly how to identify which fields to send.

 

Given the code in the reference book I commented my confusions:

 

public class AuthCallout {
   public void basicAuthCallout(){
      HttpRequest req = new HttpRequest();
      req.setEndpoint('http://www.yahoo.com'); //OK, So I obviously changed this to my website

      req.setMethod('GET');  //I'd prefer a POST, but GET is fine

      // Specify the required user name and password to access the endpoint
      // As well as the header and header information
      String username = 'myname';  //I don't need this, I plan to use other means of authentication

      String password = 'mypwd';     //I don't need this, I plan to use other means of authentication
      Blob headerValue = Blob.valueOf(username + ':' + password); //don't need this either
      String authorizationHeader = 'BASIC ' +                                   //don't need, right?

      EncodingUtil.base64Encode(headerValue);                               //do I need? I don't know.

      req.setHeader('Authorization', authorizationHeader);               //still don't need

     // Create a new http object to send the request object
      // A response object is generated as a result of the request
      Http http = new Http();            //WHAT is this?

      HTTPResponse res = http.send(req); //I don't need a response
      System.debug(res.getBody());         // I don't need this either

   }
}

 

This leaves my code at a very confusing:

 

public class AuthCallout {
   public void basicAuthCallout(){
      HttpRequest req = new HttpRequest();
      req.setEndpoint('http://www.myurl.com');

      req.setMethod('GET'); 

      Http http = new Http(); /maybe?

  }

}

 

So either I want to make a dynamic URL:

req.setEndpoint('http://www.myurl.com?name=a.name__c&phone=a.phone__c&etc=et_cetera__c');

 

OR

 

somewhere else I need to define my array variables

 

Can someone help me with this? I would be very appreciative.

Best Answer chosen by Admin (Salesforce Developers) 
cheenathcheenath

That example is for sending an HTTP request with base 64 auth.

 

Here is a simple example:

 

 //construct an HTTP request
HttpRequest req = new HttpRequest();
req.setEndpoint('http://cheenath.com/tutorial/sfdc/sample1/data.txt');
req.setMethod('GET');

//send the request
Http http = new Http();
HttpResponse res = http.send(req);

//check the response
if (res.getStatusCode() == 200) {
//success
} else {
System.debug('Callout failed: ' + res);
}