简体   繁体   English

如何在 azure function 2.0 中获取传入 HTTP POST 请求的 URL?

[英]How to get URL of incoming HTTP POST request in azure function 2.0?

I want to store URL of incoming HTTP request in variable.我想将传入 HTTP 请求的 URL 存储在变量中。 I am using Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME") but it gives the URL of my azure where azure function in hosted.我正在使用Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME")但它提供了我的 azure 的 URL,其中托管了 azure 函数。

Please refer below code:请参考以下代码:

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System.Net;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Blob;

 public static async Task<IActionResult> Run(HttpRequest req, CloudBlockBlob outputBlob)
    {
        var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data =  JsonConvert.DeserializeObject(requestBody);
        var referer = req.Headers["Referer"].ToString();

        data.Add("url",referer);
        //data.Add("url",Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME"));
        string output = JsonConvert.SerializeObject(data);

        await outputBlob.UploadTextAsync(output);
        return new OkObjectResult("Data has been received");
    }

Actual data received as below:收到的实际数据如下:

{"firstname":"Sam","lastname":"Smith","email":"sam.smith@gmail.com","dob":"1990-1-1","url":"https://xxxxxx.azurewebsites.net/api/xxxx?code=xxxxx"}

Update 0823:更新 0823:

Hey, If you still don't know how to get the url.嘿,如果您仍然不知道如何获取网址。 You can take a look of this:你可以看看这个:

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    var referer = req.Headers["Referer"].ToString();
    log.LogInformation("Referer is "+referer);
    return new OkObjectResult($"");
}

在此处输入图像描述

This is the most basic code to get the url.The circle that I circled in red is the url that sent the post.If you still have some questions, please let me know.这是获取url的最基本代码。我用红色圈出的圆圈是发帖的url。如果你还有什么问题,请告诉我。


if you want to get the incoming url in Azure Function httpTrigger, you can do like this:如果你想在 Azure Function httpTrigger 中获取传入的 url,你可以这样做:

 var referer = req.Headers["Referer"].ToString();
        log.LogInformation("Referer is "+referer);
        var host = req.Host;
        log.LogInformation("Host is " + host);

in your httptrigger,the request header have the host and the Referer,the host is the url of your httptrigger and the Referer is the url that the request comes from.在你的httptrigger中,请求头有host和Referer,host是你的httptrigger的url,Referer是请求来自的url。

Make sure your request is not from https to http,otherwise the Referer will disappear and you can't get it.确保你的请求不是从https到http,否则Referer会消失,你无法获取。 https to https or http to https is ok. https 到 https 或 http 到 https 都可以。

I have already try the question you ask.I send a request from a webapplication that is deployed on the Azure and send it to the httptrigger.Finally,I get the right url:我已经尝试过您提出的问题。我从部署在 Azure 上的 Web 应用程序发送请求并将其发送到 httptrigger。最后,我得到了正确的 url:

在此处输入图像描述


You can get the host name from the HttpRequest req object:您可以从HttpRequest req对象中获取主机名:

HostString hostString = req.Host;
string host = hostString.Host;
int? port = hostString.Port;
string fullHost = hostString.Value; 

在我的脑海中,我认为它会是这样的:

  var url = request.Scheme + "://" + request.Host + request.Path;

You can obtain the URL of the request HOST like this.您可以像这样获取请求 HOST 的 URL。

First change the HttpRequest to HttpRequestMessage and then the below two lines will give you the host.首先将HttpRequest更改为HttpRequestMessage ,然后下面两行将为您提供主机。

var context = (DefaultHttpContext) req.Properties["HttpContext"];
var host = context.Request.Host;

I have added my sample app with output.我添加了带有输出的示例应用程序。

[FunctionName("Function1")]
        public async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous,"post", Route = null)]
            HttpRequestMessage req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var context = (DefaultHttpContext) req.Properties["HttpContext"];
            var host = context.Request.Host;
            log.LogInformation(host.ToString());
            return req.CreateResponse(HttpStatusCode.Accepted);

        }

在此处输入图像描述

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

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