简体   繁体   English

表的插入脚本中的AZURE移动服务转发POST请求

[英]AZURE Mobile Service forwarding POST request in insert script of table

I'm trying to use Azure Mobile Service to process / handle GET and POST requests on an empty data table. 我正在尝试使用Azure移动服务来处理/处理空数据表上的GET和POST请求。 (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. (实际上只是使用移动服务作为传递),在此过程中,我试图将请求转发到另一个URL,并接收回响应,然后通过移动服务将其返回。 I've figured out the GET part shown below but I'm having trouble the POST part. 我已经弄清楚了下面显示的G​​ET部分,但是在POST部分遇到了麻烦。

GET Part:(Which works) GET部分:(哪个作品)

    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"); 
        }
});

}

I know the POST requires a body with the post information, but how to I get it to pass forward? 我知道POST需要带有发布信息的正文,但是如何使它向前传递?

On an insert, the body of the request will be stored in the item argument (assuming you're passing a JSON object). 在插入时,请求的正文将存储在item参数中(假设您要传递JSON对象)。 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"); 
        }
    });
}

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. 值得一提的是,如果您将移动服务作为简单的传递使用,则还可以考虑使用自定义API而不是表,在该表中您还可以应用逻辑而无需任何逻辑(空)表。

function insert(item, user, request) 
{
    var p = request.parameters;
    require('request').post({
        uri : 'http://someURL/',
        headers : {'Content-Type': 'application/json'},
        body : JSON.stringify(item)
    }, function(err, res, body){
        if (err)
        {
            request.respond(500, "INTERNAL SERVER ERROR"); 
        }
        else
        {
            request.respond(200,"Success"); 
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM