R API using Plumber under Azure API Management CORS error

Enrico Rossini 246 Reputation points
2021-04-08T10:46:50.113+00:00

I'm trying to create some APIs in R with Plumber. I have managed to publish the API on a server and I can access them via browser: Swagger is exposed without any authentication. The code is

# api.R

#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg="") {
  list(msg = paste0("The message is: '", msg, "'"))
}

#* Plot a histogram
#* @serializer png
#* @get /plot
function() {
  rand <- rnorm(100)
  hist(rand)
}

#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a, b) {
  as.numeric(a) + as.numeric(b)
}

#* @filter cors
cors <- function(res) {
    res$setHeader("Access-Control-Allow-Origin", "*")
    plumber::forward()
}

and they are working.

aYiPd.png

I want to protect them and I think to use Azure API Management service. I have created the service and added the APIs using the Swagger document. Also, I added CORS in the Inbound processing.

wzKrI.png

In Settings I didn't change anything although I should use OpenID connect.

VJzeB.png

There is a simple api /echo that requires msg as parameter. If I run the test for this api, I receive 400 Bad Request.

zjoZg.png

In the Trace I have this error for 3 times:

cors (0.020 ms) "Origin header was missing or empty and the request was classified as not cross-domain. CORS policy was not applied."

I can't find what the error is about. Also, I Enable CORS in the Developer Portal although I think it is totally unrelated.

What did I do wrong? What is the correct configuration?

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.


2 answers

Sort by: Oldest
  1. Enrico Rossini 246 Reputation points
    2021-04-13T09:11:19.28+00:00

    Thank you for your reply. I found the issue; it was in the firewall configuration. I have a different problem now.

    Was this answer helpful?


  2. Mary Letourneau 0 Reputation points
    2026-11-15T20:48:51.9366667+00:00

    Hi Enrico, glad you got this sorted! Just adding a note for anyone who lands here with the same symptoms.

    The trace message “Origin header was missing or empty and the request was classified as not cross-domain. CORS policy was not applied” looks like an error but isn’t one. It’s just APIM saying the request had no Origin header, so there was nothing for the CORS policy to do. That’s expected when you use the Test console in the portal, or call the API from Postman or curl. CORS only comes into play for cross-origin requests from a browser.

    So the CORS policy was a red herring here. The 400 was coming from somewhere else, and as you found, the culprit was the firewall configuration. APIM couldn’t properly reach the backend where the Plumber API is hosted.

    If you hit a similar 400 or timeout, check these first:

    • Is your backend allowing inbound traffic from the APIM instance’s IP addresses?
    • Does the backend URL in APIM point to the right host and port?
    • Does calling the backend directly from outside work, and does the Trace tab show the request actually reaching it?

    Once you start calling the API from a browser-based app, the CORS policy will matter, so it’s good that it’s already in place.

    Thanks for coming back and sharing the fix!

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.