Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set parameters with HTTP POST in Apex

I'm trying to set POST content using Apex. The example below sets the variables using GET

  PageReference newPage = Page.SOMEPAGE;
  SOMEPAGE.getParameters().put('id', someID);
  SOMEPAGE.getParameters().put('text', content);

Is there any way for me to set the HTTP type as POST?

like image 735
Sushant Rao Avatar asked Jun 27 '13 21:06

Sushant Rao


People also ask

How do I set parameters in HTTP request Salesforce?

You need to use req. setbody('id=' + someId '&text=' + content); as described here stackoverflow.com/questions/14551194/… dont forget to set Content-Type to application/x-www-form-urlencoded like this req. setHeader('Content-Type','application/x-www-form-urlencoded') and urlEncode your data.

What is parameterized method in Apex?

Apex, in general, is a statically-typed programming language, which means users must specify the data type for a variable before that variable can be used.

What is RestContext?

RestContext is a predefined class in apex that consist of request and response context variables. With this, I mean that I can access the whole incoming request by using the request context variable of RestContext class. RestRequest is also a predefined apex class which can be used to create/accept an HTTP request.


1 Answers

Yes but you need to use HttpRequest class.

String endpoint = 'http://www.example.com/service';
String body = 'fname=firstname&lname=lastname&age=34';
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setbody(body);
Http http = new Http();
HTTPResponse response = http.send(req);

For additional information refer to Salesforce documentation.

like image 54
Moti Korets Avatar answered Oct 15 '22 09:10

Moti Korets