如何从 Azure Function App 中的请求正文中检索字节数据
How to retrieve bytes data from request body in Azure Function App
在Python中,我将图像转换为字节。然后,我像这样将字节传递给 Azure HTTP 触发器函数应用端点 URL (Azure 门户),就像调用 Azure 认知服务时一样。
image_path = r"C:\Users\User\Desktop\bicycle.jpg"
image_data = open(image_path, "rb").read()
print(len(image_data)) # print length to compare later
url = "https://xxxx.azurewebsites.net/api/HTTPTrigger1........."
headers = {'Content-Type': 'application/octet-stream'}
response = requests.post(url, headers=headers,
data=image_data)
但是,我不知道如何在 Azure 门户上的函数应用程序中检索字节数据。我尝试了以下 (C#),但没有用。似乎 ReadToEndAsync()
不是要从请求正文中读取字节数据?还是因为HttpRequest
?
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
byte[] imageBytes = Encoding.ASCII.GetBytes(requestBody);
log.LogInformation(imageBytes.Length.ToString());
// the length logged is totally not the same with len(image_data) in Python
//ignore the following lines (not related)
return name != null
? (ActionResult)new OkObjectResult("OK")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
对此有什么想法吗?我确实知道使用 base64 字符串的解决方法,但我真的很好奇 Azure 认知服务是如何做到的!
提前致谢。
不要使用ReadToEndAsync()
,而是使用MemoryStream()
。 ReadToEndAsync()
用于读取字符串缓冲区,它可能会弄乱传入的字节数据。使用CopyToAsync()
然后将内存流转换为字节数组以保留传入的字节数据。
public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
//string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
MemoryStream ms = new MemoryStream();
await req.Body.CopyToAsync(ms);
byte[] imageBytes = ms.ToArray();
log.LogInformation(imageBytes.Length.ToString());
// ignore below (not related)
string finalString = "Upload succeeded";
Returner returnerObj = new Returner();
returnerObj.returnString = finalString;
var jsonToReturn = JsonConvert.SerializeObject(returnerObj);
return new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
};
}
public class Returner
{
public string returnString { get; set; }
}
Reference/Inspired 来自:
https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers
在Python中,我将图像转换为字节。然后,我像这样将字节传递给 Azure HTTP 触发器函数应用端点 URL (Azure 门户),就像调用 Azure 认知服务时一样。
image_path = r"C:\Users\User\Desktop\bicycle.jpg"
image_data = open(image_path, "rb").read()
print(len(image_data)) # print length to compare later
url = "https://xxxx.azurewebsites.net/api/HTTPTrigger1........."
headers = {'Content-Type': 'application/octet-stream'}
response = requests.post(url, headers=headers,
data=image_data)
但是,我不知道如何在 Azure 门户上的函数应用程序中检索字节数据。我尝试了以下 (C#),但没有用。似乎 ReadToEndAsync()
不是要从请求正文中读取字节数据?还是因为HttpRequest
?
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
byte[] imageBytes = Encoding.ASCII.GetBytes(requestBody);
log.LogInformation(imageBytes.Length.ToString());
// the length logged is totally not the same with len(image_data) in Python
//ignore the following lines (not related)
return name != null
? (ActionResult)new OkObjectResult("OK")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
对此有什么想法吗?我确实知道使用 base64 字符串的解决方法,但我真的很好奇 Azure 认知服务是如何做到的!
提前致谢。
不要使用ReadToEndAsync()
,而是使用MemoryStream()
。 ReadToEndAsync()
用于读取字符串缓冲区,它可能会弄乱传入的字节数据。使用CopyToAsync()
然后将内存流转换为字节数组以保留传入的字节数据。
public static async Task<HttpResponseMessage> Run(HttpRequest req, ILogger log)
{
//string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
MemoryStream ms = new MemoryStream();
await req.Body.CopyToAsync(ms);
byte[] imageBytes = ms.ToArray();
log.LogInformation(imageBytes.Length.ToString());
// ignore below (not related)
string finalString = "Upload succeeded";
Returner returnerObj = new Returner();
returnerObj.returnString = finalString;
var jsonToReturn = JsonConvert.SerializeObject(returnerObj);
return new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
};
}
public class Returner
{
public string returnString { get; set; }
}
Reference/Inspired 来自: https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers