Logo

dev-resources.site

for different kinds of informations.

Running HTTP requests in PhpStorm

Published at
10/10/2023
Categories
api
phpstorm
http
requests
Author
dcblog
Categories
4 categories in total
api
open
phpstorm
open
http
open
requests
open
Author
6 person written this
dcblog
open
Running HTTP requests in PhpStorm

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?

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
###
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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...
Enter fullscreen mode Exit fullscreen mode

For more details read the official docs

phpstorm Article's
30 articles in total
Favicon
How to configure PHPStorm to work with Vite - Aliases
Favicon
Cursor AI Editor vs PhpStorm - a quick Laravel comparison
Favicon
Snapshots for AI: A β€œRAG-Like” solution for programming with LLMs
Favicon
Como instalar o PHPStorm no WSL2
Favicon
Enhance PhpStorm File Templates with Velocity πŸ§ͺ
Favicon
Simple DX improvement in PHPStorm
Favicon
Interact with Docker containers without leaving PhpStorm
Favicon
Docker on WSL with PhpStorm - Best of both worlds
Favicon
Phpstorm Intellij search mode when focusing
Favicon
Simplify API Testing with PhpStorm HTTP Requests
Favicon
Running HTTP requests in PhpStorm
Favicon
PhpStorm with Docker
Favicon
How to select a File Using Ctrl+P In PHPstorm as I did in VSCode and VSCodium
Favicon
Automatic Git Commit and Push on Save using PHPStorm
Favicon
XDebug, PHPStorm, Docker, macOS Ventura
Favicon
Laravel Sail & XDebug
Favicon
Atalhos para ser mais produtivo no PHPStorm
Favicon
Comment Publier Rapidement un Projet sur GitHub
Favicon
Impressions on GitHub Copilot and PHPStorm - March 2023
Favicon
Using Laravel Sail alongside PhpStorm
Favicon
Pair programming in PHPStorm
Favicon
PHPStorm Tips #10 : Last Edit Location
Favicon
PHPStorm Tips #9 : Zoom ton code
Favicon
PHPStorm Tips #7 : Naviguer dans le YAML
Favicon
My barebones React component snippet
Favicon
PHPStorm Tips #5 : Toute la pouissance des Live Templates
Favicon
PHPStorm Tips #6 : Les contextes
Favicon
PHPStorm Tips #8 : Rechercher et ouvrir plusieurs fichiers
Favicon
PHPStorm Tips #2 : Select Occurences
Favicon
PHPStorm Tips #4 : Extend Selection

Featured ones: