What I need
I’m working with a third-party API (for the intents of this question, it’s irrelevant which one) and need to send a POST request to an external service.
I’m trying to use wp_remote_post
to achieve what I need, but there seems to be a limit to the amount of data it can send in its request body.
The issue I’m facing
It gets sent only if the body contains up to 1024 characters. One character more and the request (apparently) isn’t sent.
Debug
I’m trying to debug the issue using ngrok to listen for the POST requests at $url
, and manually setting $body
as a string of “bla bla bla (…)”. They only show up when $body
contains 1024 or less chars. I don’t get a 5xx error or any other error message. In fact, is_wp_error( $response )
returns false.
However, if I use a tool such as Postman to generate a similar POST request outside WP with more than 1024 characters in its body, ngrok receives it just fine.
- WP -> request body smaller than 1024 bytes -> ngrok = OK
- WP -> request body bigger than 1024 bytes -> ngrok = ??
- Postman -> request body smaller than 1024 bytes -> ngrok = OK
- Postman -> request body bigger than 1024 bytes -> ngrok = OK
What I tried
- Sending the same request from outside WordPress with more than 1024 bytes on its body: it works.
- A brand new WordPress install with no plugins and a default theme (twentyseventeen): it doesn’t work.
- Trying on a different PC, with the latest Fedora install, the latest Linux kernel, the latest Firefox version, the latest Docker version: didn’t work.
My setup
- Fedora Linux 24 kernel 4.11.12-100.fc24.x86_64
- Docker version 1.12.6, build 78d1802
- WordPress 4.9.5 from the official Docker image for WordPress
- Firefox 54
- ngrok 2.2.8
All tests are being made on my local machine, with WordPress 4.9.5 running inside a Docker container with the default WordPress image, and it uses the unmodified default configuration, except for the following custom php ini lines:
file_uploads = On
memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 600
The code
$response = wp_remote_post( $url,
array(
'timeout' => 60,
'redirection' => 5,
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => 'bla bla (...)' // fails if larger than 1024
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else { echo "Request sent"; }