Home  >  Q&A  >  body text

503 error when posting file to php server using formdata module using Node js and axios

I'm trying to send a file from my Node js application to a PHP server hosting an opencart application. I'm using formdata and axios modules to make requests and upload files.

My problem is that I get this error Error: Request failed with status code 503

How to solve?

This is my code in Node.js:

let form = new FormData();
form.append("file", fs.createReadStream(path.resolve(zipFilePath)), path.basename(zipFilePath));

            try {
                let response = await axios.post(endpoint, form, {
                    headers: {
                        ...form.getHeaders(),
                    },
                });

                const result = response.data;
                if (result && result.status === "success") {
                    fs.unlinkSync(zipFilePath);
                }
            } catch (e) {
                console.log(e.toString());
            }

and php code (function in controller):

public function upload() {
        header('Access-Control-Allow-Origin: *');
        
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            $this->response->setOutput(json_encode([]));
        } else {
            // process the file posted
        }
    }

P粉502608799P粉502608799259 days ago342

reply all(1)I'll reply

  • P粉436052364

    P粉4360523642024-02-27 09:01:35

    problem solved.

    The problem is that the opencart application is set to maintenance mode on the backend, but the frontend is still working normally, so it was not noticed before.

    reply
    0
  • Cancelreply