Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

petervandijck's avatar

Vapor: API call works locally but not on Vapor if sending a json

I have a REST API endpoint that I call with a few vallues. One of those values is a JSON.

  • Locally it works fine.
  • Deployed on Vapor, without the json value, or if I replace the json value with a plain string, it works fine
  • on Vapor, if I send the JSON value, it never even gets to the PHP code, and I get a 400 Bad Request response.

This is the weird part. I have added lots of logging. On Vapor, it never even gets to the PHP that receives the API call, but ONLY if I am sending a JSON value in that field. If I don't, it works fine.

I am stumped.

Any thoughts?

0 likes
4 replies
petervandijck's avatar

OK this is VERY strange.

If I send this string in one of the values, it is ok: "curly-q"

If I send "curly-[q" it is ok.

If I send "curly-{q", it never even gets to the PHP and I get a 400 Bad Request.

Somehow, curly brackets are being blocked by Vapor?

If I send this: "quote" I get a 500 internal server error, but that I also get locally... (where as the 400 errors I don't get locally)

If I send this: "quote (with just one quote) it is fine.

petervandijck's avatar
petervandijck
OP
Best Answer
Level 1

It turns out this was an issue with POSTMAN sending the curly bracket in the URL, which locally was fine but on Vapor was being rejected as a Bad Request. I switched to Insomnia instead of Postman, which seems nicer so far and doesn't default to doing this

gych's avatar

Good that you've managed to figure it out! :) Please don't forget to close your thread by marking your solution as best answer.

LaryAI's avatar
Level 58

It sounds like the issue you're encountering with your API on Vapor when sending JSON data might be related to how the JSON data is being formatted or encoded. Here are a few steps and checks you can perform to troubleshoot and potentially resolve this issue:

1. Check JSON Content-Type

Ensure that the Content-Type header is correctly set to application/json when you are making the request. This is crucial for the server to correctly interpret the payload as JSON.

2. Validate JSON Data

Before sending the JSON data, validate it to ensure it's correctly formatted. You can use online tools like JSONLint to validate your JSON structure.

3. Inspect JSON Encoding

If you are encoding the JSON data in your client code, ensure that the encoding process doesn't introduce any errors or formatting issues. Here’s a basic example of how you might encode and send JSON data using PHP with cURL:

$url = 'https://your-vapor-api-endpoint';
$data = [
    'key1' => 'value1',
    'key2' => 'value2'
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);

if (!$response) {
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

curl_close($ch);
echo $response;

4. Check Vapor Configuration

Review your Vapor configuration to ensure that there are no security settings or middleware that might be blocking JSON content. Sometimes, configurations differ between local and production environments.

5. Examine API Gateway Settings

Since Vapor uses AWS Lambda under the hood, AWS API Gateway might be intercepting the request before it reaches your application. Check if there are any settings in API Gateway that might be affecting the handling of JSON data.

6. Error Handling

Improve error handling in your API to provide more insight when something goes wrong. This can help identify if the issue is with the data being sent or how the data is being processed server-side.

7. Test with Postman or Curl

Test the API endpoint using tools like Postman or a simple curl command from your terminal. This can help isolate whether the issue is with the API itself or the way the client is sending data.

curl -X POST https://your-vapor-api-endpoint \
-H "Content-Type: application/json" \
-d '{"key1":"value1", "key2":"value2"}'

8. Review Server Logs

Since you mentioned that the logs do not show any entries when the error occurs, it might be useful to increase the logging level or add additional logging around the entry points of your application.

By following these steps, you should be able to pinpoint the issue more accurately. If the problem persists, consider reaching out to the Vapor support team with details of what you have tried.

Please or to participate in this conversation.