HttpRequest クラス
名前空間
使用方法
HttpRequest で作成されたリクエストボディ内の XML または JSON コンテンツを解析するには、XML クラスまたは JSON クラスを使用します。
例
次の例は、要求を含む認証ヘッダーの使用方法と応答の処理を示しています。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class AuthCallout {
18
19 public void basicAuthCallout(){
20 HttpRequest req = new HttpRequest();
21 req.setEndpoint('http://www.yahoo.com');
22 req.setMethod('GET');
23
24 // Specify the required user name and password to access the endpoint
25 // As well as the header and header information
26
27 String username = 'myname';
28 String password = 'mypwd';
29
30 Blob headerValue = Blob.valueOf(username + ':' + password);
31 String authorizationHeader = 'BASIC ' +
32 EncodingUtil.base64Encode(headerValue);
33 req.setHeader('Authorization', authorizationHeader);
34
35 // Create a new http object to send the request object
36 // A response object is generated as a result of the request
37
38 Http http = new Http();
39 HTTPResponse res = http.send(req);
40 System.debug(res.getBody());
41 }
42}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
- 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 - プレフィックス callout: があり、必要に応じてパスが追加された指定ログイン情報
1callout:My_Named_Credential/some_path
- 有効な URL
戻り値
型: Void
指定ログイン情報の使用方法
Apex コールアウトで指定ログイン情報をコールアウトエンドポイントとして指定するすべての認証が Salesforce によって管理されるため、コードでこれらを行う必要はありません。外部サイトへの Apex コールアウトに必要なリモートサイト設定もスキップできます。
エンドポイント URL と認証を Apex コードから切り離すことで、指定ログイン情報でコールアウトを簡単に管理できます。たとえば、エンドポイント URL が変更された場合も、指定ログイン情報を更新するだけです。その指定ログイン情報を参照するすべてのコールアウトは、コードを変更しなくても引き続き機能します。複数の組織が有る場合、各組織に同じ名前の指定ログイン情報を作成できます。それぞれの指定ログイン情報には、開発環境と本番環境の違いに対応したりするため、異なるエンドポイント URL を含めることができます。コードは指定ログイン情報の名前のみを参照するため、コードのプログラムで環境を確認することなく、同じ Apex クラスをパッケージ化してすべての組織にリリースできます。
1req.setEndpoint('callout:.__My_Named_Credential/some_path');例
1swfobject.registerObject("clippy.codeblock-6", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17HttpRequest req = new HttpRequest();
18req.setEndpoint('callout:My_Named_Credential/some_path');
19req.setMethod('GET');
20Http http = new Http();
21HTTPResponse res = http.send(req);
22System.debug(res.getBody());
23
コールアウトエンドポイントを指定ログイン情報ではなく URL としてコーディングできますが、コードで認証を処理する必要があります。この例では、基本的なパスワード認証を使用していますが、OAuth 認証ははるかに複雑であるため、指定ログイン情報で処理することをお勧めします。1swfobject.registerObject("clippy.codeblock-7", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17HttpRequest req = new HttpRequest();
18req.setEndpoint('https://my_endpoint.example.com/some_path');
19req.setMethod('GET');
20
21// Specify the required user name and password to access the endpoint
22// As well as the header and header information
23
24String username = 'myname';
25String password = 'mypwd';
26
27Blob headerValue = Blob.valueOf(username + ':' + password);
28String authorizationHeader = 'BASIC ' +
29EncodingUtil.base64Encode(headerValue);
30req.setHeader('Authorization', authorizationHeader);
31
32// Create a new http object to send the request object
33// A response object is generated as a result of the request
34
35Http http = new Http();
36HTTPResponse res = http.send(req);
37System.debug(res.getBody());
38setMethod(method)
署名
public Void setMethod(String method)
パラメータ
- method
- 型: String
- このメソッドの種別の値には、次のものがあります。
- DELETE
- GET
- HEAD
- POST
- PUT
- TRACE
戻り値
型: Void
使用方法
このメソッドは要求オプションの設定にも使用できます。
setTimeout(timeout)
署名
public Void setTimeout(Integer timeout)
パラメータ
- timeout
- 型: Integer
戻り値
型: Void
使用方法
タイムアウト値は 1 ~ 120,000 ミリ秒の間で設定します。
toString()
署名
public String toString()
戻り値
型: String