我需要对API执行POST调用以安排订单。因此我制作了一个简单的Node js应用程序。目前这个状态我收到文本/普通数据到我们的应用程序,但这不是JSON风格。
这就是我现在所拥有的:
TypeOrder=buy
Coin=BTC
AmountCoin=1
CoinPriceInEuro=100
CoinAddress=17J6W29E2q94YNg5eiaHGsNWW9oJxsWu1M
PaymentMethod=1
GeneralTermsAccepted=true
我想用JSON(比如):
{
"Email": "example1@1.nl",
"Coin": "BTC",
"CouponCode": "",
"AmountEuro": 80.0,
"AmountCoin": 1.0,
"CoinPriceInEuro": 80,
"CoinAddress": "17J6W29E2q94YNg5eiaHGsNWW9oJxsWu1M",
"TypeOrder": "buy",
"PaymentMethod": 1,
"GeneralTermsAccepted": false
}
并附上你找到的代码。
任何人都可以告诉我必须做什么才能在正确的json中获得它吗?
const express = require('express');
const http = require('http');
const app = express();
const fs=require('fs');
const hostname = '127.0.0.1';
const port = 3000;
const bodyParser = require('body-parser');
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({ extended: false })
fs.readFile('index.html', (err, html) => {
if(err) {
throw err;
}
var server = http.createServer((req, res) => {
res.statusCode=200;
res.setHeader('Content-type', 'text/html');
res.write(html);
res.end();
});
server.listen(port, hostname, () => {
console.log('Server started on port'+port);
});
// POST /login gets urlencoded bodies
app.post('http://localhost:3030/rest/v1/PostOrder', urlencodedParser, function (req, res) {
console.log(req.body);
if (!req.body) return res.sendStatus(400);
res.send('http://localhost:3030/rest/v1/PostOrder', {qs:req.query});
});
});

<!DOCTYPE html>
<html>
<body>
<form enctype="text/plain" action="http://localhost:3030/rest/v1/PostOrder" method="POST">
Buy/Sell:<br>
<input type="text" name="TypeOrder" value="buy">
<br>
Coin:<br>
<input type="text" name="Coin" value="BTC">
Amount in Coin:<br>
<input type="number" name="AmountCoin" value="1">
<br>
Coin Price in Euro:<br>
<input type="number" name="CoinPriceInEuro" value="100">
<br>
Coin address to send:<br>
<input type="text" name="CoinAddress" value="17J6W29E2q94YNg5eiaHGsNWW9oJxsWu1M">
<br>
Payment method:<br>
<input type="radio" name="PaymentMethod" value="1" checked> iDeal<br>
<input type="radio" name="PaymentMethod" value="2"> Credit Card<br>
<input type="radio" name="PaymentMethod" value="3"> PayPal<br>
<br>
Terms accepted:<br>
<input type="radio" name="GeneralTermsAccepted" value="true" checked>Ja<br>
<input type="radio" name="GeneralTermsAccepted" value="false">No<br>
<br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>
</body>
</htm
&#13;
答案 0 :(得分:0)
因此,您可以执行以下操作:
const data = `
TypeOrder=buy
Coin=BTC
AmountCoin=1
CoinPriceInEuro=100
CoinAddress=17J6W29E2q94YNg5eiaHGsNWW9oJxsWu1M
PaymentMethod=1
GeneralTermsAccepted=true`;
let parts = data.split( '\n' );
let formattedData = {};
parts.forEach( ( part ) => {
const splitData = part.split( '=' );
formatterData[ splitData[ 0 ] ] = splitData[ 1 ];
} );
这是一种非常简单的方法。您可以使用大量的库使其更加功能化或更简单。 :)
答案 1 :(得分:0)
正文数据由\n
分隔,因此您可以拆分然后遍历该数组。
payload_template
是您的空模板,将填充正文数据。<强>段强>
let body = `TypeOrder=buy
Coin=BTC
AmountCoin=1
CoinPriceInEuro=100
CoinAddress=17J6W29E2q94YNg5eiaHGsNWW9oJxsWu1M
PaymentMethod=1
GeneralTermsAccepted=true`;
let payload = {};
body.split('\n').forEach((c) => [key, payload[key]] = c.split('='));
console.log(payload);
.as-console-wrapper {
max-height: 100% !important
}
答案 2 :(得分:0)
无需在表单中将enctype指定为text / plain。
正确删除它,因为您的节点应用程序已经使用json编码器作为中间件。
答案 3 :(得分:0)
怎么样:
let str=`TypeOrder=buy
Coin=BTC
AmountCoin=1
CoinPriceInEuro=100
CoinAddress=17J6W29E2q94YNg5eiaHGsNWW9oJxsWu1M
PaymentMethod=1
GeneralTermsAccepted=false`;
let result=str.split('\n').reduce((re, v) => {
let key=v.split('=')[0],val=v.split('=')[1];
if (val.match(/^\d+\.\d+$|^\d+$/)) {
val=Number(val);
}else if(val.match(/^true$/i)){
val=true;
}else if(val.match(/^false$/i)){
val=false;
}
re[key]=val;
return re;
}, {});
console.log(result);