下面包含我的完整代码(Azure门户上的Azure功能应用程序)。要特别注意这两条线。
var jsonContent = req.Content.ReadAsStringAsync().Result;
log.LogInformation("jsonContent" + jsonContent);
当我使用右侧面板下的请求体测试函数时,jsonContent
将按原样打印在日志中。但是,在浏览器中使用函数url并使用&name=azure
附加它,jsonContent
为null,如日志中所示。
//full code
#r "Newtonsoft.Json"
using System.Net;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ILogger log)
{
// these two lines are problematic???
var jsonContent = req.Content.ReadAsStringAsync().Result;
log.LogInformation("jsonContent" + jsonContent);
// you can ignore the following lines (not related to question)
string jsonToReturn = "Hello World";
return new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
};
}
我尝试将线路改为此,但它也没有用。
var jsonContent = await req.Content.ReadAsStringAsync().Result;
错误是这样的
'string' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
无论如何,我知道一个解决方法是使用HttpRequest
而不是HttpRequestMessage
来生成jsonContent
,但我只是好奇为什么这个案例不起作用。
谁能发现我的错误?谢谢!
作为查询追加与使用请求正文不同。你可以像Python一样在Python中调用函数和请求体(只是一个例子):
reqBody = {
'customerid' : customerid,
'imgdata' : imgdata
}
headers = {
'Content-Type': 'application/json',
}
url = "https://xxxxx.azurewebsites.net/api/HTTPTrigger.............."
response = requests.post(url, headers=headers,
data=json.dumps(reqBody))
print(response.json())