[英]How to convert form data (text/plain) to json with node?
我需要執行對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
因此,您可以執行以下操作:
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 ];
} );
這是一個非常簡單的方法。 您可以使用大量的庫使它更加注重功能或更簡單。 :)
主體數據用\\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 }
無需在表單中將enctype指定為text / plain。
只需將其正確刪除即可,因為您的節點應用程序已在使用json編碼器作為中間件。
那這個呢:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.