gpt4 book ai didi

c# - Azure函数从站点获取json并将其传递给

转载 作者:行者123 更新时间:2023-12-03 05:51:22 25 4
gpt4 key购买 nike

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

所以我从 A 获取 Json 数据,该数据由 API 提供。现在,我有一个 azure 函数,我需要从 A 获取数据并返回 req.CreateResponse(HttpStatusCode.OK),其中包含来自 A< 的数据。这需要在 B 上可见(完全相同),而 B 这是我的 Azure 网址...

如何做到这一点?

我尝试执行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);
}
}

在这里,我想从另一个链接返回数据,如下所示:

{
"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"
}
}
]
}
}

最佳答案

我想你想要这样的东西:

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

关于c# - Azure函数从站点获取json并将其传递给,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47600199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com