I'm running into a small issue with something I thought was possible.
我遇到了一些我认为可行的小问题。
I want to have two express routes, one GET
route /post-data
and one POST
route /post-recieve
.
我想有两条快速路线,一条GET路线/后期数据和一条POST路线/后接收。
The code would look something like this:
代码看起来像这样:
app.get('/post-data', (req, res, next) => {
//set url to '/post-recieve' and set body / headers
})
app.post('/post-recieve', (req, res, next) => {
return res.json(res.body)
})
Now, when you visit /post-data
you should be instantly redirected to /post-recieve
except if you look in the developer console you should see that the method for the document is POST
and not a normal GET
request.
现在,当您访问/发布数据时,您应该立即重定向到/后接收,除非您查看开发人员控制台,您应该看到该文档的方法是POST而不是正常的GET请求。
Is this possible?
这可能吗?
I know you can use a library like request
to make a HTTP post request to an endpoint, but I'm talking about actually sending the user to the page via a POST
request.
我知道您可以使用像请求这样的库来向端点发出HTTP post请求,但我所说的实际上是通过POST请求将用户发送到页面。
3 个解决方案
#1
0
The only way to change the client's request method from GET to POST programmatically is to create a form containing hidden elements with method="post"
and action="/post-receive"
, then using client-side JavaScript to automatically submit the form.
以编程方式将客户端的请求方法从GET更改为POST的唯一方法是使用method =“post”和action =“/ post-receive”创建包含隐藏元素的表单,然后使用客户端JavaScript自动提交表单。
Any HTTP redirects in response to a GET request will also be GET.
响应GET请求的任何HTTP重定向也将是GET。
#2
0
This feels so dumb, but it might be the only way???
这感觉很愚蠢,但它可能是唯一的方法???
function postProxyMiddleware (url, data) {
return (req, res, next) => {
let str = []
str.push(`<form id="theForm" action="${url}" method="POST">`)
each(data, (value, key) => {
str.push(`<input type="text" name="${key}" value="${value}">`)
})
str.push(`</form>`)
str.push(`<script>`)
str.push(`document.getElementById("theForm").submit()`)
str.push(`</script>`)
return res.send(str.join(''))
}
}
app.get('/mock', postProxyMiddleware('/page', exampleHeaders))
#3
0
You can use request-promise to post the data to a url. So, initiate with this function and you can get the data in the api url
您可以使用request-promise将数据发布到URL。因此,使用此功能启动,您可以在api url中获取数据
const request = require('request');
const rp = require('request-promise');
let req = {
"param1" : "data1",
"param1" : "data2"
}
rp({
method: 'POST',
uri: 'http://localhost:3000/post-data/',
body: req,
json: true // Automatically stringifies the body to JSON
}).then(function (parsedBody) {
console.dir(parsedBody);
return parsedBody;
// POST succeeded...
})
.catch(function (err) {
console.log(err+'failed to post data.');
return err+'failed to post data.';
// POST failed...
});
Apologies If I get your question wrong.
道歉如果我的问题出错了。
#1
0
The only way to change the client's request method from GET to POST programmatically is to create a form containing hidden elements with method="post"
and action="/post-receive"
, then using client-side JavaScript to automatically submit the form.
以编程方式将客户端的请求方法从GET更改为POST的唯一方法是使用method =“post”和action =“/ post-receive”创建包含隐藏元素的表单,然后使用客户端JavaScript自动提交表单。
Any HTTP redirects in response to a GET request will also be GET.
响应GET请求的任何HTTP重定向也将是GET。
#2
0
This feels so dumb, but it might be the only way???
这感觉很愚蠢,但它可能是唯一的方法???
function postProxyMiddleware (url, data) {
return (req, res, next) => {
let str = []
str.push(`<form id="theForm" action="${url}" method="POST">`)
each(data, (value, key) => {
str.push(`<input type="text" name="${key}" value="${value}">`)
})
str.push(`</form>`)
str.push(`<script>`)
str.push(`document.getElementById("theForm").submit()`)
str.push(`</script>`)
return res.send(str.join(''))
}
}
app.get('/mock', postProxyMiddleware('/page', exampleHeaders))
#3
0
You can use request-promise to post the data to a url. So, initiate with this function and you can get the data in the api url
您可以使用request-promise将数据发布到URL。因此,使用此功能启动,您可以在api url中获取数据
const request = require('request');
const rp = require('request-promise');
let req = {
"param1" : "data1",
"param1" : "data2"
}
rp({
method: 'POST',
uri: 'http://localhost:3000/post-data/',
body: req,
json: true // Automatically stringifies the body to JSON
}).then(function (parsedBody) {
console.dir(parsedBody);
return parsedBody;
// POST succeeded...
})
.catch(function (err) {
console.log(err+'failed to post data.');
return err+'failed to post data.';
// POST failed...
});
Apologies If I get your question wrong.
道歉如果我的问题出错了。