invalid url

gopaldurgoji
Participant III

Hi All, I have three URI's - http://test.apigee.net/v1/students/13,

http://test.apigee.net/v1/students/13/present and http://test.apigee.net/v1/students/13/absent where '13' is the roll number of a student.

Im extracting the roll number by using extract variable policy and validating whether its present or not.

If not I will be raising a fault.

Suppose, if the URI is http://test.apigee.net/v1/students//present , its considering 'present' itself as a roll number due to which Im not able to raise the fault.

How can I solve the above problem.

Solved Solved
0 5 1,121
1 ACCEPTED SOLUTION

I think you need to introduce flow logic that rejects such invalid URLs.

You can do this with regular expression matches.

Like this:


  <Flows>

    <Flow name="student present-absent">
      <Request>
      </Request>
      <Response>
        <Step>
          <Name>AM-ResponsePresentAbsent</Name>
        </Step>
      </Response>
      <Condition>(proxy.pathsuffix ~~ "/students/[0-9]{1,5}/(present|absent)") and (request.verb = "GET")</Condition>
    </Flow>


    <Flow name="student">
      <Request>
      </Request>
      <Response>
        <Step>
          <Name>AM-ResponseStudent</Name>
        </Step>
      </Response>
      <Condition>(proxy.pathsuffix ~~ "/students/[0-9]{1,5}") and (request.verb = "GET")</Condition>
    </Flow>


    <Flow name="unknown request">
      <Request>
        <Step>
          <Name>RF-UnknownRequest</Name>
        </Step>
      </Request>
      <Response>
      </Response>
    </Flow>


For a full working example, see attached. apiproxy-regex-url-match.zip

Here's an example of me invoking that proxy. you can see that some URLs get rejected:

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students
{
  "error" : {
    "code" : 404.01,
    "message" : "that request was unknown; try a different request."
  }
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students/13
{
    "query" : "student"
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students/13/present
{
    "query" : "student present/absent"
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students/13/absent
{
    "query" : "student present/absent"
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students//absent
{
  "error" : {
    "code" : 404.01,
    "message" : "that request was unknown; try a different request."
  }
}

View solution in original post

5 REPLIES 5

I think you need to introduce flow logic that rejects such invalid URLs.

You can do this with regular expression matches.

Like this:


  <Flows>

    <Flow name="student present-absent">
      <Request>
      </Request>
      <Response>
        <Step>
          <Name>AM-ResponsePresentAbsent</Name>
        </Step>
      </Response>
      <Condition>(proxy.pathsuffix ~~ "/students/[0-9]{1,5}/(present|absent)") and (request.verb = "GET")</Condition>
    </Flow>


    <Flow name="student">
      <Request>
      </Request>
      <Response>
        <Step>
          <Name>AM-ResponseStudent</Name>
        </Step>
      </Response>
      <Condition>(proxy.pathsuffix ~~ "/students/[0-9]{1,5}") and (request.verb = "GET")</Condition>
    </Flow>


    <Flow name="unknown request">
      <Request>
        <Step>
          <Name>RF-UnknownRequest</Name>
        </Step>
      </Request>
      <Response>
      </Response>
    </Flow>


For a full working example, see attached. apiproxy-regex-url-match.zip

Here's an example of me invoking that proxy. you can see that some URLs get rejected:

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students
{
  "error" : {
    "code" : 404.01,
    "message" : "that request was unknown; try a different request."
  }
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students/13
{
    "query" : "student"
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students/13/present
{
    "query" : "student present/absent"
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students/13/absent
{
    "query" : "student present/absent"
}

$ curl https://${ORG}-${ENV}.apigee.net/regex-url-match/students//absent
{
  "error" : {
    "code" : 404.01,
    "message" : "that request was unknown; try a different request."
  }
}

@Dino thanks for the info its working fine.

One doubt what if the Roll No can be an alphanumeric or only numeric for example C12 or 12?

How to solve the above problem then?

Regular Expressions are a powerful language . You can learn more about them at regular expressions.info.

Some simple examples:

description regex
a string of between 1-5 numeric digits [0-9]{1,5}
a string of between 5-7 uppercase alpha or numeric digits [A-Z0-9]{5,7}
a string of exactly 1 uppercase alpha followed by 4 numeric digits [A-Z][0-9]{4}
a string of 0 or 1 uppercase alpha followed by 4 numeric digits [A-Z]?[0-9]{4}
an A or a C followed by 4 numeric digits [AC][0-9]{4}
either an A or a C or nothing, followed by 4 numeric digits [AC]?[0-9]{4}
a string of 16 mixed uppercase and lowercase alpha [a-zA-Z]{16}

There are obviously lots more possibilities.

To work out your regular expressions, you can try some online regex testers in your browser. I like this one.

@Dino

Thanks for the info 🙂

Hello! I am very glad that I got to your programming forum. I am also a programmer and face the fact that in my code I detect bugs, of course this work is far from easy, but I can do it. And of course, one of the problems (if we are talking about sites) is the wrong url, because of course it makes work difficult. I searched a lot on this topic on the Internet, but still decided to order a couple of work on the site https://assignment-helpers.co.uk, and I learned a lot for myself. If you add up, we can say that a lot of practice will allow you to create better code. And if you want to order several articles on this topic, go to the site that I wrote in this text.