Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpRequest is null when I run an azure function

I want to upload files into azure blob storage using AzureFunctions in c#.

So this is my azure function :

public static class AppartementFunc
{

    [Function("AppartementFunc")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
    {
        string Connection = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
        string containerName = Environment.GetEnvironmentVariable("ContainerName");
        Stream myBlob = new MemoryStream();
        var file = req.Form.Files["File"];
        myBlob = file.OpenReadStream();
        var blobClient = new BlobContainerClient(Connection, containerName);
        var blob = blobClient.GetBlobClient(file.FileName);
        await blob.UploadAsync(myBlob);
        return new OkObjectResult("file uploaded successfylly");
    }
}

I tried to test this function from postman like this :

enter image description here

but this line var file = req.Form.Files["File"] trigger an error :

`System.NullReferenceException: 'Object reference not set to an instance of an object.'

req was null.`

I dont know why the HttpRequest req is NULL ! Can anyone help me with that ?

like image 458
Faouzeya Avatar asked Jun 28 '26 19:06

Faouzeya


1 Answers

My guess is you're using code for Azure Functions "in-process" hosting model while the application is actually hosted via "isolated worker" model.

For Azure Functions Isolated Worker hosting model, the HttpTrigger parameter should be of type HttpRequestData and HTTP response return types should be Task<HttpResponseData>

Differences Between In-Process & Isolated Worker Models

Guide for running C# Azure Functions in an isolated worker process

like image 137
Moho Avatar answered Jul 01 '26 09:07

Moho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!