AZURE Mobile Service在表的插入脚本中转发POST请求 [英] AZURE Mobile Service forwarding POST request in insert script of table
问题描述
我正在尝试使用Azure移动服务来处理/处理空数据表上的GET和POST请求。 (实际上只是使用移动服务作为传递)
作为其中的一部分,我正在尝试将请求转发到另一个URL并收到响应并通过移动服务返回。我已经找到了下面显示的GET部分,但我遇到了POST部分的问题。
I'm trying to use Azure Mobile Service to process / handle GET and POST requests on an empty data table. (really just using the mobile service as a pass through) As part of this I'm trying to forward the request to another url and receive the response back and return it via mobile service. I've figured out the GET part shown below but I'm having trouble the POST part.
获取部分:(哪个有效)
GET Part:(Which works)
function read(query, user, request)
{
var p = request.parameters;
var httpRequest = require('request');
var url = 'http://someURL/'+ p.ssoid;
httpRequest.get(url, function(err, response, body)
{
if (err)
{
request.respond(500, "INTERNAL SERVER ERROR");
}
else
{
request.respond(200,JSON.parse(body) );
}
});
}
邮政编码:(不起作用)
Post Code:(Does not work)
function insert(item, user, request)
{
var p = request.parameters;
require('request').post({
uri:'http://someURL/',
headers:{'content-type': 'application/json'},
body:p.body
},function(err,res,body){
if (err)
{
request.respond(500, "INTERNAL SERVER ERROR");
}
else
{
request.respond(200,"Success");
}
});
}
我知道POST需要一个带有帖子信息的正文,但是如何让它向前传递?
I know the POST requires a body with the post information, but how to I get it to pass forward?
推荐答案
在插入时,请求的主体将存储在 item
参数(假设您传递的是JSON对象)。所以你的函数看起来像这样:
On an insert, the body of the request will be stored in the item
argument (assuming you're passing a JSON object). So your function would look something like this:
function insert(item, user, request)
{
var p = request.parameters;
require('request').post({
uri : 'http://someURL/',
headers : {'Content-Type': 'application/json'},
body : item
}, function(err, res, body){
if (err)
{
request.respond(500, "INTERNAL SERVER ERROR");
}
else
{
request.respond(200,"Success");
}
});
}
在相关说明中,如果您将移动服务用作简单的传递,您还可以考虑使用自定义API而不是表,在这里您也可以应用逻辑而不需要任何(空)表。
On a related note, if you're using the mobile service as a simple pass-through, you can also consider using a custom API instead of a table, where you can also apply your logic without having any (empty) table behind it.
这篇关于AZURE Mobile Service在表的插入脚本中转发POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!