If I understand correctly, you have a script that adds scripts through the zap api but having trouble doing so when in docker. One potential problem which I would first like to ensure is not causing problems is the difference in permissions between the outside user and the zap user in docker. Please take a quick look at the Dockerfile section of
https://augment1security.com/cicd/cicd-with-owasp-zap-docker-and-pipeline-scripting-part-1/ to see what I mean. After that, see if you are still getting that 400 response code when you execute the script (that adds scripts) again.
Another question I have, to understand better, is , when you say "I already tested the script locally and it works perfectly", you are running the script against desktop zap?