Code Monkey home page Code Monkey logo

Comments (9)

zackyang000 avatar zackyang000 commented on August 19, 2024

request data 只接受 JSON 格式的数据, JavaScript object 是不行的, 你可以尝试:

POST /orders

{"custom":{"id":"1","name":"john"},"orderItems":[{"quantity":10,"product":{"id":"1","name":"com","price":2.1}}]}

from node-odata.

crcren avatar crcren commented on August 19, 2024

Thx,it's done.

from node-odata.

crcren avatar crcren commented on August 19, 2024

还有一个问题:
.before(function (entity) {...}) 这个before方法里可以对request内容进行更改吗?这样可以不必重定义post等方法? 怎么做?

from node-odata.

zackyang000 avatar zackyang000 commented on August 19, 2024

可以的.

server.resource('orders', order)
.post().before(function (entity) {
    entity.custom.name = "zack";
});

from node-odata.

fungwan avatar fungwan commented on August 19, 2024

@TossShinHwa 你好,如果request data的json数据里面包含了中文,POST会失败。都是英文的话不会返回ERROR。试试下面这段代码呢?看你那边会不会。

var http = require('http');

var bookBody = {
    "title": "黑客与画家",
    "price":9.89
}

var bookBodyString = JSON.stringify(bookBody);
var options = {
    host: 'localhost',
    port:'3000',
    path: '/books',
    method: 'POST',
    headers: {
        'Accept': '/',
        'Content-Type':'application/json',
        'Content-Length': bookBodyString.length
    }
};

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function(data) {
        console.log(data);
    });

    res.on('err',function(data){
        console.log(data);
    });
});


req.write(bookBodyString);
req.end();`

from node-odata.

zackyang000 avatar zackyang000 commented on August 19, 2024

这么取长度是不对的.

'Content-Length': bookBodyString.length

你可以考虑去掉这个参数, 让他自动计算, 就不会报错了.

from node-odata.

fungwan avatar fungwan commented on August 19, 2024

谢谢,果然是中文的坑。或者改成下面获取长度也可以避免。
Buffer.byteLength(bookBodyString, 'utf8')

from node-odata.

zackyang000 avatar zackyang000 commented on August 19, 2024

Great! 我之前都没有手动去处理过 req 的 length 问题, 学习了.

Sent from my iPhone

On May 18, 2016, at 21:41, 冯云 [email protected] wrote:

谢谢,果然是中文的坑。或者改成下面获取长度也可以避免。
Buffer.byteLength(bookBodyString, 'utf8')


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

from node-odata.

crcren avatar crcren commented on August 19, 2024

node test.js
{"title":"黑客与画家","price":9.89,"id":"7f9224d6-3b8c-4a14-9606-b665e17cec7d"}

我刚刚看到,调试的时候也是报错误,后台我对比curl,去掉了 content-length,就ok了

from node-odata.

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.