CNode

请问如何POST cnode社区的api?

问答
Hhuan1043269994发布于9 年前最后回复9 年前6 回复4121 浏览0 收藏
var postData = JSON.stringify({
  'accesstoken': 'xxxx',
})

var options = {
  hostname: 'https://cnodejs.org',
  path: '/api/v1',
  port: 443,
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
  
var req = https.request(options, (res) => {
    console.log(`STATUS: ${res.statusCode}`);
    console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
      console.log(`主体: ${chunk}`);
    });
    res.on('end', () => {
      console.log('响应中已无数据');
    })
  })
}```
无响应
查看回复

回复 (6)

Z
zhanzhenzhen#3·9 年前

哎,为什么有这么多人喜欢把Content-Type设为application/x-www-form-urlencoded?JSON的Content-Typeapplication/json

J
jiangli373#4·9 年前

untitled1.png

你没有写具体的请求地址吧 path: '/api/v1',

这和一楼说的Content-Type没有什么关系

T
tomoya92#5·9 年前

我记得有个request类库可以直接请求呀, 没必要写这么多吧

N
nullcc#6·9 年前

搞请求用superagent呀

参与回复
登录后即可参与回复。登录