Node js应该解析curl数组

时间:2018-08-10 13:49:31

标签: node.js curl

我想使用curl向节点js端点发出POST请求。 curl请求应该发送一个值数组。 Node js应该能够使用该数组。

这是我的卷曲要求

json='{"userEmail": "nas@nas.com", "assetUrls": ["test.com"]}'
echo $json | curl -i -X POST -d @- -H "Content-Type: application/json" -H "Authorization: Bearer eyJ4NXUiOiJpbXNfbmExLXN0ZzEta2V5LTEuY2VyIiwiYWxnIjoiUlMyNTYifQ.eyJpZCI6IjE1MzM5MDg3MzQ4MzBfZjI4MTBiNWYtODEzZC00ZWI0LWJmOGQtOTcyOTEyZjYwZDVhX3VlMSIsImNsaWVudF9pZCI6Ik1hcnZlbFdlYlRlc3QxIiwidXNlcl9pZCI6IkE2NzcxNUY0NTY5OTVFMjk3RjAwMDEwMUBBZG9iZUlEIiwidHlwZSI6ImFjY2Vzc190b2tlbiIsImFzIjoiaW1zLW5hMS1zdGcxIiwiZmciOiJTVkxMU0NTRVhQNTc3NzM3TjdDTU9BSUFEUT09PT09PSIsInNpZCI6IjE1MzM4NzQyNjMyNzVfMjNlNTFlMTktZTIyNy00MTk3LWE0NDctYzRhNmU1OGJmZTZjX3VlMSIsIm1vaSI6IjdhZjIyMTliIiwiYyI6IjBCVGh1L0lLYkVaVmFnWGg4VGVhNFE9PSIsImV4cGlyZXNfaW4iOiI4NjQwMDAwMCIsInNjb3BlIjoiQWRvYmVJRCxvcGVuaWQsY3JlYXRpdmVfc2RrLGduYXYsc2FvLnNwYXJrLGFkZGl0aW9uYWxfaW5mby5wcm9qZWN0ZWRQcm9kdWN0Q29udGV4dCx0a19wbGF0Zm9ybSIsImNyZWF0ZWRfYXQiOiIxNTMzOTA4NzM0ODMwIn0.eANA8LDCBiWk-kM6TSLTfjx8uoTcU5wFlOKyo8qZP5TYRPDSkwefvJ6SHKwsIWI4SkHeiyWcuzU28_od3AMLvcmJQp4RXHK9kDiy0rjnAZm2xJe9XjM91hOqmwtVpnO3s-re2oJC7to0HboLtD62GwTWeu2Vq74vSCveSc80smGIWBeLB4cCPtvGR_rEXGFRuV8YJ6vdcBi51YJg52qrniaRi0x0Zj3XjuCwn1nZKj46uzMdRdI9b56E3F37rFRF7K8-rw7FgFaZWj6-ZMsHflRTd9tlk_dYCqXNc8p9FP7DmP-fXkLFVFqB0ByWhLVo26Tj4gfZky5jQFJBYYoRMA" https://marvel.adobe.com:8033/lightroom-hackaton

在节点js端我应该做什么,以便它正确处理curl请求并可以读取值数组“ assetUrls”?请注意,内容类型为json

1 个答案:

答案 0 :(得分:0)

这是基于http的超级简单nodejs服务器

const http = require('http');

const handler = (req, res) => {
  if (req.method === 'POST') {
    let body = '';
    req.on('data', chunk => body += chunk.toString());
    req.on('end', () => {
        const post = JSON.parse(body);
        // Now you can do something else with post.assetUrls
        console.log(post.assetUrls);
        // ...
    });
  }
};

const app = http.createServer(handler);
app.listen(8000);

现在您可以使用下一个命令node index.js运行它,现在您可以执行curl请求。

但是请注意,它是非常简单的实现,对于实际项目,可能必须使用之类的东西,并且您还必须实现授权中间件。