POST request with JSON body

POST request with JSON body

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

POST request with JSON body

When working with REST APIs like the Google Blogger API, the most common interaction pattern involves sending a POST request to create a new resource. In this specific case, you are creating a new blog post. The core mechanism relies on HTTP semantics: the POST method tells the server to expect data in the request body, and that data must be formatted according to the API's requirements. For Google APIs, that format is almost exclusively JSON.

The example provided by Google outlines the raw HTTP structure required for this operation. To translate this into a practical application using PHP, you must replicate three critical components: the target URI, the Content-Type header, and the authorization token. You cannot simply pass the payload as query parameters; the server expects the body to be deserialized as JSON.

Configuring the Request Headers

The most frequent stumbling block for developers is handling the Content-Type header. If you send a raw string without declaring it as JSON, the API will likely return a 400 Bad Request error because it cannot parse the incoming data. In PHP, using the curl extension—the standard tool for HTTP requests outside of frameworks—requires explicit header configuration.

You must set Content-Type: application/json. Without this, the server assumes you might be sending form-encoded data (like a login POST) or raw XML, neither of which matches the Blogger API's expectations. Additionally, the Authorization header is mandatory for almost all Google APIs. This header carries the OAuth 2.0 access token obtained during the client-side authentication flow. The token must be included exactly as provided by the OAuth provider; it cannot be appended to the URL or sent in a different format.

Constructing the JSON Payload

The body of the request defines the properties of the new post. In the Google example, the JSON object includes a kind field identifying the resource type, a blog object containing the blog ID, and then the actual content fields like title and content.

When constructing this in PHP, it is crucial to ensure the payload is a valid JSON string before sending it. While you could manually concatenate strings, using json_encode() is the safer approach as it handles escaping of special characters (like <b> tags or newlines) automatically. However, be cautious about how you pass the resulting string to curl. You must ensure that CURLOPT_POSTFIELDS receives a raw string, not an array of data. Sending an array causes curl to automatically encode the fields as application/x-www-form-urlencoded, which will break the request.

Handling HTML Entities in Content

One specific detail from the Google example is the usage of <b> tags within the content string: "With &lt;b&gt;exciting&lt;/b&gt; content...". This illustrates a common point of confusion regarding how APIs handle HTML versus escaped strings.

If you are populating the content field with user input or data from your own database, you must decide whether the API expects raw HTML or escaped entities. In this specific Blogger API example, the JSON shows escaped entities (&lt;, &gt;). This implies that if you were to send raw HTML directly (e.g., <b>exciting</b>), the API might interpret the angle brackets as malformed JSON syntax and reject the request.

However, in many modern APIs, including recent versions of Blogger, passing raw HTML is often accepted because the JSON parser handles string boundaries correctly. If you are building a robust application, it is usually safer to send raw HTML if your backend stores it that way, but you must verify the specific API version documentation. Sending escaped entities is never wrong for JSON, but sending raw HTML might be required depending on how the backend processes the content field.

Integration with Laravel and Frameworks

Developers often ask how this translates to modern frameworks like Laravel. While the underlying HTTP mechanics remain the same, frameworks abstract away curl. Libraries like Illuminate\Http\Request or dedicated API client packages handle headers and JSON encoding automatically. However, understanding the raw requirements is essential for debugging when framework methods behave unexpectedly. If you are using a package to send requests, ensure that the accepts and asJson() methods are configured to match the Google Blogger API's expectations before dispatching the request.

Troubleshooting Common Errors

If your POST request fails after setting up headers and payloads correctly, check the response status code. A 401 Unauthorized means the OAuth token is missing or invalid. A 400 Bad Request usually indicates a malformed JSON body or an incorrect Content-Type. Always inspect the response body returned by the API; it often contains a descriptive error message explaining exactly which field in your JSON payload caused the rejection.

Our network