2

In my PayPalService.cs file, I have a public method in the class that calls an Azure HTTP triggered method in PayPalFunctions.cs file that keeps failing due to an error: "Method not Allowed".

This is the calling function:

public async Task<OrderResponse> CreateOrderAsync(PaymentOrderRequest request)
{
    var response = await _httpClient.PostAsJsonAsync("api/CreateOrder", request).ConfigureAwait(false);
     response.EnsureSuccessStatusCode();
     return await response.Content.ReadFromJsonAsync<OrderResponse>();
}

Trigger function is defined as:

[Function("CreateOrder")]
public async Task<HttpResponseData> CreateOrderAsync(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req)
{
    _logger.LogInformation("Create Order");

    // Read request body
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync().ConfigureAwait(false);
    var orderRequest = JsonConvert.DeserializeObject<PaymentOrderRequest>(requestBody);
    // ... etc
}

So the PostAsJsonAsync call to the CreateOrder function is being rejected due to:

Response status code does not indicate success: 405 (Method Not Allowed) "Method: POST, RequestUri: 'http://localhost:5047/api/CreateOrder', Version: 1.1, Content: System.Net.Http.Json.JsonContent, Headers: { Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 }"

All searches in Google indicate the PostAsJsonAsync call to the function is failing because the httptrigger method does not include "post". But it DOES.

Any ideas as to what is happening?

Further background - the call was originally being made from a WebAssembly page to the SSR app. I am trying to make it all work in SSR only, for security reasons.

2
  • Are you certain that the API endpoint is expecting a post request? Looking at your error I would assume that the problem is revolving around the fact that you are using a post request, not that it may be missing somewhere. Commented Oct 27 at 3:30
  • 1
    I would focus on the API call itself here. 405 tells that the resource (api/CreateOrder) exists but it doesn't support the target method which is POST in this case. Might the API be expecting PUT for some reason? Commented Oct 27 at 4:03

1 Answer 1

0

If your Azure Function is running in Isolated mode, the Functions host runs separately from your ASP.NET Core SSR app.

The 405 means the route was matched, but the HTTP method was not allowed. In most cases, your SSR app and Azure Function host run on different ports, and the request is going to the wrong host.

  • Your SSR app runs at something like http://localhost:5047
  • Your Function app likely runs at a different port, such as http://localhost:7071

So your _httpClient.PostAsJsonAsync("api/CreateOrder") is actually calling your SSR app, not the Function host. The SSR app may have an GET: /api/{id} route that matches your request, so it returns 405 instead of 404.

By default, the function route is based on the function name (CreateOrder), not api/CreateOrder. The api prefix might be unnecessary unless your host is configured that way. Try calling the endpoint without the prefix:

await _httpClient.PostAsJsonAsync("CreateOrder", request);

or confirm in the Function logs if /api is part of the route.

Fix

Option 1 — Separate Function App

Give _httpClient the correct base address:

_httpClient.BaseAddress = new Uri("http://localhost:7071/");

Then your call:

await _httpClient.PostAsJsonAsync("api/CreateOrder", request);

Point your _httpClient to the correct port (e.g., 7071), and verify whether /api/ is actually needed in your request path.

Option 2 — Integrated into SSR (.NET 8+)

Make sure your Program.cs includes:

app.MapFunctionsEndpoints();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.