Running HTTP requests in PhpStorm
Tags: api, phpstorm, http, requests
Published: 2023-10-10 19:56:39

---
title: Running HTTP requests in PhpStorm
published: true
date: 2023-10-10 19:48:38 UTC
tags: api, phpstorm, http, requests
canonical_url: https://dcblog.dev/running-http-requests-in-phpstorm
cover_image: https://cdn.hashnode.com/res/hashnode/image/upload/v1696967470741/7dc9261e-25c1-44c2-a428-c45e14ad2060.jpeg
---

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696958133738/b3b55ccc-3b13-4674-9fa4-ef42178aa824.png)

For a scratch file select the `HTTP Request` type.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696958227364/9573ce5a-8175-4566-a11b-6de43f648c20.png)

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/](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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696966405315/afac14bf-0a55-47cc-a4c0-0600c563377c.png)

## 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](https://www.jetbrains.com/help/phpstorm/http-client-in-product-code-editor.html)