Skip to content

Error 406 or 500 when executing function returning HttpResponseMessage<byte[]> on Azure CLI #69

@sirmamedical

Description

@sirmamedical

I am trying to implement a file download using Azure function in java. The function is:

@FunctionName("FileDownload")
@HttpOutput(name="$return", dataType = "binary")
public HttpResponseMessage<byte[]> download(@HttpTrigger(name = "req", methods = {"get"}, route = "download", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage req, ExecutionContext context) {
    try {
        byte[] bytes = "Simple text file to test download".getBytes("UTF-8");

        HttpResponseMessage<byte[]> response = req.createResponse(200, bytes);
        response.addHeader("Content-Type", "application/octet-stream");
//            response.addHeader("Content-Length", "" + bytes.length);
        response.addHeader("Content-Disposition", "attachment; filename=myfile.txt");
        return response;

    } catch (UnsupportedEncodingException e) {
        context.getLogger().warning("Invalid encoding");
    }

    return req.createResponse(200, "Invalid encoding!".getBytes());
}

Function is executed successfully as it is reported in the log:

[3/15/2018 6:45:49 AM] Function started (Id=d6b40f17-7217-4f6e-8c19-00e3025f3821)
[3/15/2018 6:45:49 AM] Executing 'Functions.FileDownload' (Reason='This function was programmatically called via the host APIs.', Id=d6b40f17-7217-4f6e-8c19-00e3025f3821)
[3/15/2018 6:45:49 AM] Function "FileDownload" (ID: 02ea5ace-c7ff-47b1-816a-03a845973936) invoked by Java Worker
[3/15/2018 6:45:49 AM] Function completed (Success, Id=d6b40f17-7217-4f6e-8c19-00e3025f3821, Duration=57ms)
[3/15/2018 6:45:49 AM] Executed 'Functions.FileDownload' (Succeeded, Id=d6b40f17-7217-4f6e-8c19-00e3025f3821)

The execution in curl produces the following:

$ curl -v http://localhost:7071/api/download
* timeout on name lookup is not supported
*   Trying ::1...
* Connected to localhost (::1) port 7071 (#0)
> GET /api/download HTTP/1.1
> Host: localhost:7071
> User-Agent: curl/7.48.0
> Accept: */*
>
< HTTP/1.1 406 Not Acceptable
< Date: Thu, 15 Mar 2018 07:56:57 GMT
< Content-Type: application/octet-stream
< Server: Kestrel
< Content-Length: 0
< Content-Disposition: attachment; filename=myfile.txt
<
* Connection #0 to host localhost left intact

When I uncomment the line where Content-Length header is set then it produces error 500.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions