Azure函数从站点获取json并将其传递给它

时间:2017-12-01 19:26:22

标签: c# json azure azure-functions

我正在尝试将数据从链接A传递到azure函数,这些函数只需要从链接A获取数据并将数据返回给它自己。

所以我从req.CreateResponse(HttpStatusCode.OK)获取Json数据,该数据由API提供。现在,我有一个天蓝色的功能,我需要从A获取数据并使用B中的数据返回B。这需要在await req.Content.ReadAsStringAsync()[FunctionName("CreateRadioAPI")] public static async Task<HttpResponseMessage> CreateRadioAPIAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/radio/stats/info")]HttpRequestMessage req, TraceWriter log) { try { //dynamic body = await req.Content.ReadAsStringAsync(); return req.CreateResponse(HttpStatusCode.OK); } catch (Exception ex) { log.Error("An error occured: ", ex); return req.CreateResponse(HttpStatusCode.InternalServerError); } } 上可见(完全相同)这里是我的Azure网址...

怎么做呢?

我试图做{ "data": { "listeners": 65, "song": "Ofenbach - Katchi (Ofenbach vs. Nick Waterhouse)", "live_dj": ".-RRxd-.", "mini_rooster": [], "song_history": [ { "playedat": 1512154754, "title": "Ofenbach - Katchi (Ofenbach vs. Nick Waterhouse)", "metadata": { "tit2": "Ofenbach - Katchi (Ofenbach vs. Nick Waterhouse)" } }, { "playedat": 1512154548, "title": "Cam'ron - Hey Ma", "metadata": { "tit2": "Cam'ron - Hey Ma" } }, { "playedat": 1512154335, "title": "San Holo - I Still See Your Face", "metadata": { "tit2": "San Holo - I Still See Your Face" } }, { "playedat": 1512151761, "title": "Sunnieday Mixtape Dyna (5)", "metadata": { "tit2": "Sunnieday Mixtape Dyna (5)" } }, { "playedat": 1512151162, "title": "Merk & Kremont - Sad Story (Out Of Luck)", "metadata": { "tit2": "Merk & Kremont - Sad Story (Out Of Luck)" } }, { "playedat": 1512150866, "title": "Martin Garrix - So Far Away", "metadata": { "tit2": "Martin Garrix - So Far Away" } }, { "playedat": 1512150701, "title": "John de Sohn - Hum With Me", "metadata": { "tit2": "John de Sohn - Hum With Me" } }, { "playedat": 1512150345, "title": "Zak Abel - All I Ever Do (Is Say Goodbye)", "metadata": { "tit2": "Zak Abel - All I Ever Do (Is Say Goodbye)" } }, { "playedat": 1512150139, "title": "Dua Lipa - New rules", "metadata": { "tit2": "Dua Lipa - New rules" } }, { "playedat": 1512149995, "title": "Garmiani - Fogo", "metadata": { "tit2": "Garmiani - Fogo" } } ] } } ,但这没有做任何事情......

所以,我的功能应用程序现在看起来像这样:

[InvoiceDate] <= [EndDate] AND [InvoiceDate] >= "1/1/" & Year([EndDate])

在这里,我想从其他链接返回数据,如下所示:

[InvoiceDate] BETWEEN "1/1/" & Year([EndDate]) AND [EndDate]

1 个答案:

答案 0 :(得分:2)

我想你想要这样的东西:

private static HttpClient client = new HttpClient();

[FunctionName("CreateRadioAPI")]
public static async Task<HttpResponseMessage> CreateRadioAPIAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/radio/stats/info")]HttpRequestMessage req, TraceWriter log)
{
    try
    {
        var responseFromA = await client.GetAsync("https://yoursite.com/a.json");
        var body = await responseFromA.Content.ReadAsStringAsync();
        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(body, Encoding.UTF8, "application/json");
        return response;
    }
    catch (Exception ex)
    {
        log.Error("An error occured: ", ex);
        return req.CreateResponse(HttpStatusCode.InternalServerError);
    }
}