Put request with simple string as request body

前端 未结 8 554
谎友^
谎友^ 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:36

    this worked for me.

    let content = 'Hello world';
    
    static apicall(content) {
    return axios({
      url: `url`,
      method: "put",
      data: content
     });
    }
    
    apicall()
    .then((response) => {
       console.log("success",response.data)
    }
    .error( () => console.log('error'));
    
    0 讨论(0)
  • 2020-12-24 14:41

    simply put in headers 'Content-Type': 'application/json' and the sent data in body JSON.stringify(string)

    0 讨论(0)
  • 2020-12-24 14:42

    This worked for me:

    export function modalSave(name,id){
      console.log('modalChanges action ' + name+id);  
    
      return {
        type: 'EDIT',
        payload: new Promise((resolve, reject) => {
          const value = {
            Name: name,
            ID: id,
          } 
    
          axios({
            method: 'put',
            url: 'http://localhost:53203/api/values',
            data: value,
            config: { headers: {'Content-Type': 'multipart/form-data' }}
          })
           .then(function (response) {
             if (response.status === 200) {
               console.log("Update Success");
               resolve();
             }
           })
           .catch(function (response) {
             console.log(response);
             resolve();
           });
        })
      };
    }
    
    0 讨论(0)
  • 2020-12-24 14:44

    I solved this by overriding the default Content-Type:

    const config = { headers: {'Content-Type': 'application/json'} };
    axios.put(url, content, config).then(response => {
        ...
    });
    

    Based on m experience, the default Conent-Type is application/x-www-form-urlencoded for strings, and application/json for objects (including arrays). Your server probably expects JSON.

    0 讨论(0)
  • 2020-12-24 14:44

    axios.put(url,{body},{headers:{}})

    example:

    const body = {title: "what!"}
    const api = {
      apikey: "safhjsdflajksdfh",
      Authorization: "Basic bwejdkfhasjk"
    }
    
    axios.put('https://api.xxx.net/xx', body, {headers: api})
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题