Giter VIP home page Giter VIP logo

Comments (8)

 avatar commented on July 22, 2024

request.body.readAll().toString()

from fibjs.

LuoZijun avatar LuoZijun commented on July 22, 2024

@xicilion 使用你说的这个 request.body.readAll().toString() 好像不行。

代码

var http = require('http');
var mq = require('mq');

var application = function (req){
    var protocol = req.protocol;
    var method = req.method;
    var request_uri = req.address;
    var query_string = req.queryString;

    var headers = req.headers.toJSON();
    var cookies = req.cookies.toJSON();
    // content-type 支持有问题,目前不支持 application/json
    var form = req.form.toJSON();

    // *** HTTP BODY ***
    var body = req.body.readAll().toString();
    var body_size = req.length;

    var remote_port = req.stream.remotePort;
    var remote_address = req.stream.remoteAddress;
    var local_port = req.stream.localPort;
    var local_address = req.stream.localAddress;

    console.log( body );
};

var server = new http.Server(8080, application);
server.run();

HTTP Request

curl -X PUT -d '{"uuuu":"34564p910521"}' "http://127.0.0.1:8080/path1/cgi.py?pwd=3465&abc=bbc" -H 'TEST: ASDAS'

错误提示

HttpHandler: TypeError: Cannot read property 'toString' of null at application (app.js:17:34)

from fibjs.

 avatar commented on July 22, 2024

你之前成功获取过 form,所以 body 内容已经被读取出来了。如果你还想重读,可以先 req.body.rewind();

from fibjs.

LuoZijun avatar LuoZijun commented on July 22, 2024

@xicilion 谢谢。
另外一个问题,由于我自己需要去 headers 里面读取 content-type ,但是 底层实现似乎没有考虑大小写问题,也没有对数据进行修整。所以我必须得先在 js 里面遍历所有的 key 才能取到我想要的 'content-type' 的值。

对于这个问题,能否在底层 比如统一小写,或者 首字母大写呢 ?
:))

from fibjs.

LuoZijun avatar LuoZijun commented on July 22, 2024

@xicilion 不对哦,读取了 form 之后,再使用 req.body.rewind(); 重读 返回的是 undefined .

from fibjs.

 avatar commented on July 22, 2024

req.body.rewind() 不是读操作,是指针复位操作,之后再用 readAll 读取。
headers 查询是不区分大小写的,你测一下。

from fibjs.

 avatar commented on July 22, 2024

你区分了大小写,是因为你把 header toJSON 转换成 JS 的 Object 了。

from fibjs.

LuoZijun avatar LuoZijun commented on July 22, 2024

@xicilion Thx.

from fibjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.