Response
HTTP レスポンスの定義を作成するクラス処理中に例外が発生した場合、応答オブジェクト内のほとんどのプロパティは空です。
// Gets the content of a PasteBin paste, assuming that we have received its key (`pasteBinKey`) in a prior request.
const http = require('@jetbrains/youtrack-scripting-api/http');
const connection = new http.Connection('http://pastebin.com/raw/');
connection.addHeader({name: 'Content-Type', value: 'text/plain'});
const response = connection.getSync(pasteBinKey, '');
if (response && response.code === 200) {
let text = '';
response.headers.forEach(function(header) {
text += header.name + ': ' + header.value + '\n';
});
text += '\n' + response.response;
issue.addComment(text);
}
Properties
メソッド
json
json()
レスポンスボディを JSON として解析します。これは JSON レスポンスを読み取る推奨方法です。ボディが文字列の場合、JSON.parse(response.response) と同等です。ボディがすでにオブジェクトに解析されている場合、このメソッドはそれを変更せずに返します。JSON.parse と同様に、ボディに有効な JSON が含まれていない場合、このメソッドは例外をスローします。ボディを解析する前に、isSuccess または code の値を確認してください。
2024.2 から利用可能
- 戻り値
タイプ
説明
オブジェクト、配列
解析されたレスポンスボディ。
- サンプル
- // Reads a JSON response from a third-party service. // The API key is stored in the app settings as a secret, so it is passed to the `addHeader` method as is. // Concatenating the API key with a string produces a string that contains a mask instead of the API key. const http = require('@jetbrains/youtrack-scripting-api/http'); const connection = new http.Connection('https://api.example.com'); connection.addHeader('X-Api-Key', ctx.settings.apiKey); const response = connection.getSync('customers/' + customerId); if (response.isSuccess) { const customer = response.json(); ctx.issue.addComment('Customer plan: ' + customer.plan); } else { console.warn('Request failed: ' + response.code + ' ' + response.response); }
2026 年 9 月 10 日