メインコンテンツまでスキップ
バージョン: 20 R7

OutgoingMessage

4D.OutgoingMessage クラスを使うと、アプリケーションの関数がREST リクエスト に応答して返すメッセージを作成することができます。 レスポンスが4D.OutgoingMessage 型であった場合、REST サーバーはオブジェクトを返すのではなく、OutgoingMessage クラスのオブジェクトインスタンスを返します。

一般的に、このクラスは、onHttpGet キーワードで宣言され、HTTP GET リクエストを処理するように設計された関数の中で使用することができます。 このようなリクエストは、例えば、ファイルのダウンロード、画像の生成、ダウンロードなどの機能を実装するためや、ブラウザを介して任意のコンテンツタイプを受信するために使用されます。

このクラスのインスタンスは4D Server 上にビルドされ、4D REST サーバー によってのみブラウザに送信することができます。 このクラスを使用することで、HTTP 以外のテクノロジー(例: モバイルなど)を使用することができます。 このクラスを使用することで、HTTP 以外のテクノロジー(例: モバイルなど)を使用することができます。

履歴
リリース内容
20 R7クラスを追加

例題

この例題では、getFile() 関数はDatastore クラス に実装されており、REST リクエストによって呼び出すことができます。 ここでの目的は、testFile.pdf ファイルをリクエストへのレスポンスとして返すことです: ここでの目的は、testFile.pdf ファイルをリクエストへのレスポンスとして返すことです:

Class extends DataStoreImplementation

exposed onHTTPGet Function getFile() : 4D.OutgoingMessage

var $result:=4D.OutgoingMessage.new()
var $file:=File("/RESOURCES/testFile.pdf")

$result.setBody($file.getContent()) // これはバイナリーのコンテンツ
$result.setHeader("Content-Type"; "application/pdf")
return $result

OutgoingMessage Object

4D.OutgoingMessage オブジェクトは以下のプロパティと関数を提供します:

body : any
送信されるメッセージ本文
headers : Object
送信されるメッセージのカレントのヘッダーがキー/値のペアとして格納されてます
.setBody( body : any )
body 引数をメッセージの本文として設定します
.setHeader( key : Text ; value : Text )
sets the outgoing message header key with the provided value
.setStatus( status : Integer )
sets the status property with the given status
status : Integer
the current status of the outgoing message

4D.OutgoingMessage オブジェクトは共有不可 オブジェクトです。

.body

body : any

説明

.body プロパティには送信されるメッセージ本文が格納されています。 .body プロパティでは以下のデータ型がサポートされます:

  • テキスト
  • blob
  • object
  • image

.body プロパティは読み書き可能です。

.body プロパティはまた、setBody() 関数を使用しても設定することができます。この場合、content-type ヘッダーは自動的に設定されます。

.headers

headers : Object

説明

.headers プロパティには送信されるメッセージのカレントのヘッダーがキー/値のペアとして格納されてます。

.headers プロパティは、読み出し専用です。 ヘッダーを設定するには、setHeader() 関数を使用します。

.setBody()

.setBody( body : any )

引数説明
bodyany->送信メッセージの本文

説明

.setBody() 関数は、body 引数をメッセージの本文として設定します。

body 引数では以下のデータ型がサポートされています:

  • Text
  • BLOB
  • Object
  • ピクチャー

この関数が使用された場合、content-type ヘッダーはbody 引数の型に応じて自動的に設定されます:

  • Content-Type: 本文がテキストの場合にはtext/plain
  • Content-Type: 本文がBlob の場合にはapplication/octet-stream
  • Content-Type: 本文がオブジェクトの場合にはapplication/json
  • Content-Type: image/jpeg、image/gif... (本文が画像の場合)

body がサポートされていない値の型だった場合、エラーが返されます。

.setHeader()

.setHeader( key : Text ; value : Text )

引数説明
keyText->設定するヘッダープロパティ
valueText->ヘッダープロパティの値

説明

The .setHeader() function sets the outgoing message header key with the provided value. If both parameters are not Text values, an error is raised.

When returning a 4D.OutgoingMessage object instance, 4D automatically sets some headers (e.g. Set-Cookie with WASID4D=... and 4DSID__ProjectName_=....).

If you set a value for the "Content-Type" header key, make sure you call this function after the call to setBody(), because setBody() automatically fills this header. For a list of "Content-Type" header values, please refer to the WEB SEND BLOB documentation.

.setStatus()

.setStatus( status : Integer )

引数説明
statusInteger->Status to set

説明

The .setStatus() function sets the status property with the given status.

If status is not an integer value, an error is raised.

For a list of HTTP status codes, please refer the HTTP status code list on Wikipedia.

.status

status : Integer

説明

The .status property contains the current status of the outgoing message. This property can be set using the setStatus() function.