Score:0

Extract XML Request body variable into an nginx variable

in flag

I have an XML request body where I want to extract a value like:

<?xml version="1.0" ?><Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><Body><myvariable>123</myvariable></Body></Envelope>

I want to extract the value in the tag, assign it to the nginx variable and then if the value is set, use it for rate limiting.

So far as far as my research goes, the only way to extract a regex based group from the $request_body variable is through the map like:

map $request_body $student_id {
    "~.*<myvariable>(.*)</myvariable>.*" $s;
    default 0;
}

My understanding is the $s variable will hold the capture group and then I can use it like:

if ($s = 123)
{
    rate_limit;
}

But the above is not working. Have someone done something like the above? Any pointers will be really appreciated.

bjoster avatar
cn flag
You do NOT want to parse XML using regex.
void_in avatar
in flag
I was only going to extract a single variable from the XML. So far I am able to achieve that.
bjoster avatar
cn flag
... as long as <myvariable> is a one liner. And I *think* `<` and `>` are metacharacters, so do they have to be escaped (to `\<` and `\>`) to match them literally?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.