このすみノート

Webエンジニアが技術や趣味を書くブログです。

Software Designで紹介されてるPostmanのCode snippet機能が興味深かった

Software Design 2022年8月号を読んでいます。 Web API特集に興味があり、買いました。

まだ第1特集の第1章を読み終えたところなのですが、PostmanのCode snippet機能がおもしろかったので紹介します。

Code snippet機能とは

PostmanのCode snippet機能は、各言語向けのAPIクライアントコードを出力する機能です。

私の例では、画面右側にCode snippetが表示されています。

なお、私のMac環境ではHomebrewを使って、アプリケーションをインストールしました。

brew install postman --cask

cURL

curl --location --request GET 'https://postman-echo.com/post?foo=1&bar=2' \
--header 'Cookie: sails.sid=s%3AQaXGU...' \
--data-raw ''

PHP - Guzzle

<?php
$client = new Client();
$headers = [
  'Cookie' => 'sails.sid=s%3AqWC...'
];
$body = '';
$request = new Request('GET', 'https://postman-echo.com/post?foo=1&bar=2', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();

NodeJS - Axios

var axios = require('axios');
var data = '';

var config = {
  method: 'get',
  url: 'https://postman-echo.com/post?foo=1&bar=2',
  headers: { 
    'Cookie': 'sails.sid=s%3AqWCLbuZyvCTJI_Ii1XkLTpUYv5V8eWGh.BQlOC%2FQfIPVZqqohXr5%2BLBj8TJXfbomPBqBKBIEys0U'
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

各種プログラミング言語に対応している

  • C#
  • Dart
  • Go
  • Java
  • JavaScript
  • C
  • NodeJS
  • PHP
  • Python
  • R
  • Ruby
  • Swift

をはじめとする、主要プログラミング言語に対応しています。 個人的には、まったく使ったことないプログラミング言語のソースコードを見るのが楽しいです。

あとがき

Software Design 2022年8月号のWeb API特集、まだ途中ですが内容も良さげなので、じっくり読んで知識を吸収していきたいです。