javajsonazure-functionscontent-type

Setting the Content-Type to application/json in a Java Azure Function


I have developed a Java Azure function that is triggered by an HTTP Request, performs a query to CosmosDB, and return the retrieved data to the caller (front end). The problem is that the Content-Type of the Azure Function is plain text instead of application/json.

this is the code of my azure function:

@FunctionName("backorders")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET},
                authLevel = AuthorizationLevel.ANONYMOUS)
                HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");
        try{     
            createConnection();
        } catch (Exception e){
            System.out.println("An error occured when connecting to the DB");
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when connecting to the DB").build();
        }
        ArrayList<String> supplyPlannerIds = new ArrayList<>();
        try{
            Map<String,String> headers = request.getQueryParameters();
            System.out.println(headers.values());
            String param = headers.getOrDefault("spids", "");
            if(param.equals("")){
                System.out.println("The list of Supply Planner IDs can not be empty");
                return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("The list of Supply Planner IDs can not be empty").build();
            } else {
                System.out.println("Parsing the request body");
                supplyPlannerIds = new ArrayList<String>(Arrays.asList(param.split(",")));
            }
        } catch (Exception e){
            System.out.println("An error occured when fetching the SupplyPlannerIds");
            return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occured when retrieving the list of SupplyPlannerIds from the request").build();
        }
        BackorderListRepo orderRepo = new BackorderListRepo(container, supplyPlannerIds);
        ArrayList<Backorder> orders = orderRepo.retrieveBackorders();
        //String json = new Gson().toJson(orders);
        return request.createResponseBuilder(HttpStatus.OK).body(orders).build();   
    }

this is my local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "java"
  }
}

this is my host.json

{
  "version": "2.0",
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  } 
}

in particular when I call it using Postman this is what I get postman call

I tried the following line of code but it didn't work String json = new Gson().toJson(orders);

I also tried the following but it didn't work request.getHeaders().put("content-type", "application/json");

thanks a lot in advance for your appreciated help :)


Solution

  • You were nearly there: in HTTP there are separated headers for request and response, and you tried to add a request header instead of a response header.

    Try this in your last statement:

    return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(orders).build();