Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

asp.net - Deserialize DeepObject Querystring Keys in Azure Functions / Javascript

We are constructing an API with Azure Functions, and the spec calls for DeepObject references in the GET request's querystring. So, the structure looks like https://example.com/api/persons?name[first]=Todd. The expectation is that some of the query keys may be DeepObject references, others will be flat.

This is a pattern that apparently Express can handle, but Azure Functions uses an ASP.NET router. The expectation is that the reference above should deserialize into req.params: { name: { first: "Todd" } }. However, instead the output looks like req.params: { "name[first]": "Todd" }.

I would really love to not have to regex/search each key, so does anyone know if:

  1. There's a config/flag in ASP.NET to support this structure?
  2. There's a good pattern in Javascript to deserialize this in some functional -- or at least non-idiosyncratic -- way?

Note: Anyone who suggest some use of the eval() method will not be selected. But for playing you will take home a comment with a Nineties reference, because that was the last decade the use of the that method was considered acceptable. :stuck_out_tongue_winking_eye:

question from:https://stackoverflow.com/questions/65602209/deserialize-deepobject-querystring-keys-in-azure-functions-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For this problem, I don't think we can change some configuration to support this structure. What we can do is to implement in code by ourselves.

Here is my function code:

module.exports = async function (context, req) {
    console.log("======query url is:" + req.url);
    const result = queryStringToJSON(req.url);
    console.log(result);

    context.res = {
        body: "success"
    };
}

function queryStringToJSON(queryUrl) {
    if(queryUrl.indexOf('?') > -1){
      var queryString = queryUrl.split('?')[1];
    }
    var pairs = queryString.split('&');
    var result = {};
    pairs.forEach(function(pair) {
        if (pair.indexOf('[') > -1) {
            var nameObj = {};
            var firstObj = {};
            var nameStr = pair.substring(0, pair.indexOf('['));
            var firstStr = pair.substring(pair.indexOf('[')+1, pair.indexOf(']'));
            firstObj[firstStr] = pair.split('=')[1];
            nameObj[nameStr] = firstObj;
            Object.assign(result, nameObj);
        }else {
            pair = pair.split('=');
            result[pair[0]] = decodeURIComponent(pair[1] || '');
        }
    });
    return result;
}

Start the function project, I request it with http://localhost:7071/api/HttpTrigger1?name[first]=Todd&[email protected]. The result shows:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...