gpt4 book ai didi

javascript - 如何使用 Node 将表单数据(文本/纯文本)转换为json?

转载 作者:太空宇宙 更新时间:2023-11-04 01:53:44 30 4
gpt4 key购买 nike

我需要对 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

最佳答案

正文数据由 \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
}

关于javascript - 如何使用 Node 将表单数据(文本/纯文本)转换为json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727585/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com