Running HTTP requests in PhpStorm

David Carr

3 min read - 10th Oct, 2023

2Articles1Week http Requests PhpStorm APIs

Running API tests you may be tempted to open PostMan or another API client, did you know, you can run HTTP requests from inside PhpStorm directly?

Here's a video that covers different content whilst showing API usage in PhpStorm

When developing a new API, you will want to run the API endpoints. Being able to run them without leaving PhpStorm speeds up your workflow.

To start a request file, you can either select a HTTP Request file when using the new file or create a new scratch file.

For a scratch file select the HTTP Request type.

In either case a file ending in .http for example scratch.http You can name the file anything you like and have multiple files.

The difference between having .http files in your projects and a scratch file is a scratch file is only stored in your .idea folder and not inside your project files, (outside of version control.

Demo API

Throughout this post, I will be using a sample public API https://reqres.in/

Request Format

To create a request enter the type of request (GET, POST, PUT, PATCH, DELETE) followed by the URL to fetch.

You can also set headers by typing them in the format of key:value

If you need to pass through a body then type that out with a line break in a json format.

Use ### to separate each request.


Method Request-URI HTTP-Version
Header-field: Header-value

Request-Body
###

Get Request

To create a GET request type GET followed by the URL to fetch.

You can also set headers by typing them in the format of key:value

GET https://reqres.in/api/users

Press the play button to the left of the request to run. This executes the request and shows the API response.

POST Request

In this example, I will create a new user by sending a POST request to the URL with a payload containing a name and job.

POST https://reqres.in/api/users

{
    "name": "morpheus",
    "job": "leader"
}

PATCH/PUT Request

In this example, I will update a user by sending a PATCH request to the URL with a payload containing a name and job.

PUT can also be used

PATCH https://reqres.in/api/users/2

{
    "name": "joe"
}

Delete Request

In this example, I will delete a user by sending a DELETE request to the URL.

DELETE https://reqres.in/api/users/2

Authorization header

If you need to authenticate with an API say in the format of a bearer token this is done by passing in an authorization header:


GET http://domain.com/v1.0/contacts
Accept: application/json
Content-Type: application/json
Authorization: Bearer 45|vAc8...

For more details read the official docs

1 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.