HttpRequest クラス
名前空間
使用方法
HttpRequest で作成されたリクエストボディ内の XML または JSON コンテンツを解析するには、XML クラスまたは JSON クラスを使用します。
例
次の例は、要求に認証ヘッダーを使用する方法と応答の処理を示しています。
1public class AuthCallout {
2
3 public void basicAuthCallout(){
4 HttpRequest req = new HttpRequest();
5 req.setEndpoint('http://www.yahoo.com');
6 req.setMethod('GET');
7
8 // Specify the required user name and password to access the endpoint
9 // As well as the header and header information
10
11 String username = 'myname';
12 String password = 'mypwd';
13
14 Blob headerValue = Blob.valueOf(username + ':' + password);
15 String authorizationHeader = 'Basic ' +
16 EncodingUtil.base64Encode(headerValue);
17 req.setHeader('Authorization', authorizationHeader);
18
19 // Create a new http object to send the request object
20 // A response object is generated as a result of the request
21
22 Http http = new Http();
23 HTTPResponse res = http.send(req);
24 System.debug(res.getBody());
25 }
26}HttpRequest のメソッド
HttpRequest のメソッドは次のとおりです。すべてインスタンスメソッドです。
getBodyDocument()
署名
public Dom.Document getBodyDocument()
戻り値
型: Dom.Document
例
このメソッドを次のショートカットとして使用します。
1String xml = httpRequest.getBody();
2Dom.Document domDoc = new Dom.Document(xml);getMethod()
署名
public String getMethod()
戻り値
型: String
使用方法
次は、戻り値の例です。
- DELETE
- GET
- HEAD
- PATCH
- POST
- PUT
- TRACE
setBody(body)
署名
public Void setBody(String body)
パラメーター
- body
- 型: String
戻り値
型: Void
使用方法
制限: 同期 Apex の場合は 6 MB、非同期 Apex の場合は 12 MB。
HTTP 要求のサイズおよび応答のサイズは、ヒープサイズの合計に含まれます。
setBodyAsBlob(body)
署名
public Void setBodyAsBlob(Blob body)
パラメーター
- body
- 型: Blob
戻り値
型: Void
使用方法
制限: 同期 Apex の場合は 6 MB、非同期 Apex の場合は 12 MB。
HTTP 要求のサイズおよび応答のサイズは、ヒープサイズの合計に含まれます。
setBodyDocument(document)
署名
public Void setBodyDocument(Dom.Document document)
パラメーター
- document
- 型: Dom.Document
戻り値
型: Void
使用方法
制限: 同期 Apex の場合は 6 MB、非同期 Apex の場合は 12 MB。
setClientCertificateName(certDevName)
署名
public Void setClientCertificateName(String certDevName)
パラメーター
- certDevName
- 型: String
戻り値
型: Void
使用方法
「HTTP 要求での証明書の使用」を参照してください。
setCompressed(flag)
署名
public Void setCompressed(Boolean flag)
パラメーター
- flag
- 型: Boolean
戻り値
型: Void
setEndpoint(endpoint)
署名
public Void setEndpoint(String endpoint)
パラメーター
- endpoint
- 型: String
- エンドポイントに対して可能な値は、次のとおりです。
- エンドポイント URL
1https://my_endpoint.example.com/some_path - 指定ログイン情報 URL (スキーム callout、指定ログイン情報の名前、必要に応じて追加されたパスを含む)
1callout:My_Named_Credential/some_path
- エンドポイント URL
戻り値
型: Void
setMethod(method)
署名
public Void setMethod(String method)
パラメーター
- method
- 型: String
- このメソッドの種別の可能な値には、次のものがあります。
- DELETE
- GET
- HEAD
- PATCH
- POST
- PUT
- TRACE
戻り値
型: Void
使用方法
このメソッドは要求オプションの設定にも使用できます。
setTimeout(timeout)
署名
public Void setTimeout(Integer timeout)
パラメーター
- timeout
- 型: Integer
戻り値
型: Void
toString()
署名
public String toString()
戻り値
型: String