Put request with simple string as request body

前端 未结 8 705
谎友^
谎友^ 2020-12-24 13:52

When I execute the following code from my browser the server gives me 400 and complains that the request body is missing. Anybody got a clue about how I can pass a simple st

8条回答
  •  无人及你
    2020-12-24 14:48

    This works for me (code called from node js repl):

    const axios = require("axios");
    
    axios
        .put(
            "http://localhost:4000/api/token", 
            "mytoken", 
            {headers: {"Content-Type": "text/plain"}}
        )
        .then(r => console.log(r.status))
        .catch(e => console.log(e));
    

    Logs: 200

    And this is my request handler (I am using restify):

    function handleToken(req, res) {
        if(typeof req.body === "string" && req.body.length > 3) {
            res.send(200);
        } else {
            res.send(400);
        }
    }
    

    Content-Type header is important here.

提交回复
热议问题