Making HTTP Requests
Learn how to create and send HTTP requests using the Instructor HTTP client API.
The Instructor HTTP client API provides a flexible and consistent way to create and send HTTP requests across different client implementations. This chapter covers the details of building and customizing HTTP requests.
Creating Requests
All HTTP requests are created using the HttpClientRequest
class, which encapsulates the various components of an HTTP request.
Basic Request Creation
The constructor for HttpClientRequest
takes several parameters:
The parameters are:
url
: The URL to send the request to (string)method
: The HTTP method to use (string)headers
: An associative array of HTTP headers (array)body
: The request body, which can be a string or an array (mixed)options
: Additional options for the request (array)
Request Methods
Once you’ve created a request, you can access its properties using the following methods:
Modifying Requests
You can also modify a request after it’s been created:
Note that the with*
methods return a new request instance rather than modifying the original one.
HTTP Methods
The HTTP method is specified as a string in the HttpClientRequest
constructor. The library supports all standard HTTP methods:
GET Requests
GET requests are used to retrieve data from a server:
For GET requests with query parameters, include them in the URL:
POST Requests
POST requests are used to create new resources or submit data:
PUT Requests
PUT requests are used to update existing resources:
PATCH Requests
PATCH requests are used to partially update resources:
DELETE Requests
DELETE requests are used to remove resources:
Other Methods
The library also supports other HTTP methods like HEAD, OPTIONS, etc. Just specify the method name as a string:
Setting Headers
HTTP headers are specified as an associative array where keys are header names and values are header values:
Common Headers
Some commonly used HTTP headers include:
- Content-Type: Specifies the format of the request body
- Accept: Indicates what response format the client can understand
- Authorization: Provides authentication credentials
- User-Agent: Identifies the client application
- Cache-Control: Directives for caching mechanisms
- Accept-Language: Indicates the preferred language
Request Body
The request body can be provided in two ways:
Array Body (JSON)
If you provide an array as the request body, it will automatically be converted to a JSON string:
When using an array for the body, you should set the Content-Type
header to application/json
.
String Body
You can also provide the body as a raw string:
This approach is useful for other content types:
Working with Request Body
The body is managed by the HttpRequestBody
class, which provides methods to access the body in different formats:
Request Options
The options
parameter allows you to specify additional options for the request:
Available Options
Currently, the main supported option is:
stream
: When set totrue
, enables streaming response handling
You can check if a request is configured for streaming:
Example: Streaming Request
Here’s how to create a request for a streaming API:
In the following chapters, we’ll explore how to handle responses, including streaming responses, and how to use more advanced features like request pools and middleware.