Azure Web PubSub Function - validate method receiving 400 status code

96 Views Asked by At

I'm working on an Azure Function using Web PubSub and encountering an issue with the validate method. The function is expected to handle connection validation with Abuse protection, but it's consistently returning a 400 status code.

Here's the code snippet for the validate method:

   [Function("validate")]
   [OpenApiOperation("validate", "Azure PubSub - Connection")]
   public static HttpResponseData Validate(
   [HttpTrigger(AuthorizationLevel.Function, "options")] HttpRequestData req,
   [WebPubSubContextInput] WebPubSubContext wpsReq)
   {
      return BuildHttpResponseData(req, wpsReq.Response);
   }

   private static HttpResponseData BuildHttpResponseData(HttpRequestData request, 
   WebPubSubSimpleResponse wpsResponse)
   {
       var response = request.CreateResponse();
       response.StatusCode = (HttpStatusCode)wpsResponse.Status;
       response.Body = response.Body;
       foreach (var header in wpsResponse.Headers)
       {
           response.Headers.Add(header.Key, header.Value);
       }
       return response;
     }

The function is designed to return an HttpResponseData object, and I'm using the WebPubSubContext for handling Web PubSub-related operations.

However, whenever I trigger this function, it returns a 400 status code. I've verified that the request is correctly reaching the function, but something seems off with the response. wpsReq.Response.Status is 400 always.

Live tracing response

1

There are 1 best solutions below

0
On

I have added the response handling method within the BuildHttpResponseData to fix the 400 status code issue in the Azure Web PubSub Function's validate method.

Code :

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
using System.Net;

public static class WebPubSubFunctions
{
    [Function("validate")]
    public static HttpResponseData Validate(
        [HttpTrigger(AuthorizationLevel.Function, "options")] HttpRequestData req,
        [WebPubSubContextInput] WebPubSubContext wpsReq = null)
    {
        return BuildHttpResponseData(req, wpsReq?.Response);
    }

    private static HttpResponseData BuildHttpResponseData(HttpRequestData req, HttpResponseMessage wpsResponse)
    {
        var response = req.CreateResponse();
        response.StatusCode = (HttpStatusCode)(wpsResponse?.StatusCode ?? HttpStatusCode.OK);

        if (wpsResponse != null)
        {
            var contentStream = wpsResponse.Content.ReadAsStreamAsync().Result; 

            response.Body = contentStream;

            foreach (var header in wpsResponse.Headers)
            {
                response.Headers.Add(header.Key, header.Value);
            }
        }

        return response;
    }

    private class WebPubSubContextInputAttribute : Attribute
    {
    }
}

Output :

It ran successfully as below.

enter image description here

enter image description here