javascriptnode.jsnode-fetch

400 bad request error posting thru node-fetch


const fetch = require("node-fetch").default;

    let iApi = express.Router();

    
        iApi.post("/update/:name/:value", async (req, res) => {
                   
                    const name = req.params["name"];
                    const value = req.params["value"];
            
                    const headers = { ...req.headers };
                    const url = `${process.env.URL}/name/${name}/value/${value}`;
            
                    const options = {
                        url: url,
                        method: "post",
                        headers: headers,
                        gzip: true,
                    };
            

                     try {
                        const response = await fetch(url, options);
                        handleResponseNoBody(
                          response.ok,
                          response.status,
                          response.body,
                          res,
                          url
                        );
                      } catch (err) {
                        handleResponseNoBody(false, 500, err, res, url);
                      }
                });

function handleResponseNoBody(ok, status, data, res, url) {
    const loggerOpts = Object.assign({}, LOG_DEFAULTS, {
        endpoint_url: url,
        application: "app1",
    });

    if (!ok) {
        loggerOpts.description = url + " error retrieving endpoint";
        loggerOpts.response_obj = { status_code: 500 };
        loggerOpts.err = data;
        logger.error(loggerOpts);
        res.status(status).send(data);
    } else if (status === 200) {
        loggerOpts.description = `${url} return 200`;
        loggerOpts.response_obj = { status_code: 200 };
        loggerOpts.response_body = data;
        logger.info(loggerOpts);
        res.status(status).send(data);
    } else {
        loggerOpts.description = url + " endpoint did not return 200 status";
        loggerOpts.error = data;
        loggerOpts.response_obj = { status_code: status };
        logger.info(loggerOpts);
        res.status(status).send(data);
    }
}

This code works and gives 200 when "," (comma) or string "tab" is passed as the value and works as expected, but when I try to pass "%7C" ( | - pipe ) it gives 400 bad request, I am not why that is the case can anyone help me with this? much appreciated.

https://xxx/name/NAME/value/, - works https://xxx/name/NAME/value/tab - works https://xxx/name/NAME/value/%7C - does not work


Solution

  • When you receive the %7C route parameter in req.params.value, it is already decoded to "|".

    When constructing your upstream request URL, you should ensure it is encoded properly using encodeURIComponent()

    const url = `${process.env.URL}/name/${encodeURIComponent(name)}/value/${encodeURIComponent(value)}`;
    

    Note that native fetch() is an option in Node.js. I highly recommend you switch to it if using v18 or newer.