Creating an HTTP Request
The REST Client can be found under the Tools | Test RESTful Web Service menu. It opens in a new tool window where we can craft HTTP requests that we can invoke and inspect the results for.
In the REST Client tool window, we can specify the HTTP method, the host and port to connect to (either an IP address or a hostname, can be local and on the Internet), the path we want to send the request to, headers, request parameters and the request body. Let's skip all that for now and create a GET request to "http://www.google.com". Note that if we wanted to send along cookies, we could craft these in the Cookies tab.
When we click the Run button in the toolbar (or press Ctrl+Enter), the request will be executed and the Response tab will open. In there, we can see the response body as HTML, XML, JSON or "raw", without any formatting. We can also export it to a file or open it in the browser.
Here's an example of a response that returns JSON:
The Cookies tab shows us the keys and values of all cookies that have been returned by the remote server. These cookies can be modified or deleted, and will be sent to the server on subsequent requests.
If we're interested in the response headers, we can see those in the Response Headers tab.
Generate Authorization Header
Some endpoints will require authorization. We can add the Authorization header manually when building our request. For example when consuming a service that requires an OAuth bearer token to be present, we can add it for our request:
For Basic authentication, PhpStorm can generate the header for us. Clicking the Generate Authorization Header icon in the toolbar lets us enter a username and password for which the IDE will generate a Basic authentication header.
As we've seen in the previous example, cookies that are returned by the remote server will be visible under the Cookies tab and can be edited. We can also add our own cookie values if needed for testing a service.
Clicking the + icon or pressing Alt+Insert (CMD+N on Mac OS X) will add a new cookie entry in which we can provide:
- The name of the cookie
- The value of the cookie
- The domain for which the cookie is valid
- The path for which the cookies is valid
- The cookie expiry date
While opened, PhpStorm keeps track of requests that have been made using the built-in REST client. Previously executed requests can be run again, and we can import/export requests as well.
When an HTTP request has been executed, the REST Client will keep it available from the Recent Requests list, available from the toolbar. We can replay HTTP requests that have been issued in the past, making it easier to work with a series of HTTP requests and switch between them.
Clicking the previous requests will populate the Request and Cookies tabs with the values that have been used before.
Importing and Exporting Requests for re-use
Every HTTP request that is created through the built-in REST Client can be exported and imported again later on. This allows us to create a series of HTTP requests that can be used during development, whenever needed. Create it once, export and reuse. Exporting an HTTP request can be done from the toolbar.
Requests are stored in an XML file that we can edit, if necessary. We can also add these exported requests to VCS so team members can benefit from them during development or for testing certain things. Here's an example exported request that queries our JetBrains GitHub organization for code that contains the word "PhpStorm":
Importing saved requests can be done from the toolbar as well. All we have to do is pick the file and PhpStorm will update the Request and Cookies tab with the request parameters from the saved request.
Configuring Proxy Settings
If a proxy server has to be used to connect to the host that we want to test, we can configure one by clicking the Settings icon in the toolbar. This will bring up a dialog in which we can specify proxy server settings.
PhpStorm supports autodiscovering proxies and can work with both HTTP and SOCKS proxy servers, including those that require authentication.