使用x-www-form-urlencoded提取API

时间:2018-11-12 17:44:18

标签: reactjs fetch put

我想获取put API。我和邮递员做了测试,并且可以工作。我只是不知道为什么我的代码不起作用。我想将x-www-form-urlencoded与具有1个参数'isRaining':true的主体一起使用。网址为http://thutrongtts.ml:3000/states。它意外地工作了一次,但是经过一点编辑后,尽管我尝试修复它,但再也无法工作

toggleRaining(){
        var formData = new FormData();
        formData.append('isRaining', true);
        var options = {
            'method': 'PUT',
            'headers': {
                "Content-Type": "application/x-www-form-urlencoded",
                "cache-control": "no-cache"
            },
            'body': formData
        };
        fetch('http://thutrongtts.ml:3000/states',options).then(resp => resp.json());
    }

这是邮递员测试代码

var qs = require("querystring");
var http = require("http");

var options = {
  "method": "PUT",
  "hostname": [
    "thutrongtts",
    "ml"
  ],
  "port": "3000",
  "path": [
    "states"
  ],
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded",
    "cache-control": "no-cache",
    "Postman-Token": "73bd58f4-bbf6-44d9-9d01-2a3e8ff9746a"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(qs.stringify({ isRaining: 'true', undefined: undefined }));
req.end();

0 个答案:

没有答案