在Apex中使用HTTP POST设置参数

Sus*_*Rao 8 salesforce force.com

我正在尝试使用Apex设置POST内容.下面的示例使用GET设置变量

  PageReference newPage = Page.SOMEPAGE;
  SOMEPAGE.getParameters().put('id', someID);
  SOMEPAGE.getParameters().put('text', content);
Run Code Online (Sandbox Code Playgroud)

有什么办法可以将HTTP类型设置为POST吗?

Mot*_*ets 16

是的,但您需要使用HttpRequest类.

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);
Run Code Online (Sandbox Code Playgroud)

有关其他信息,请参阅 Salesforce文档.

  • 不需要.你需要使用req.setbody('id ='+ someId'&text ='+ content); 如此处所述http://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request不要忘记将Content-Type设置为application/x-www-form-urlencoded like这个req.setHeader('Content-Type','application/x-www-form-urlencoded')和urlEncode你的数据. (5认同)