Query parameters vs json body?

Options
I'm not quite clear on how I set query parameters in the url, vs creating a JSON body in a POST request.

Which would this create, and how do I create the other one for this example?
[image.png]

Comments

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    The params section of the external API request is flexible to handle both Query Parameters and JSON request body. 

    Path parameters would need to go on the URL line. A Path parameter example would be:

    https://api.example.com/parameterA/parameterB/parameterC
  • Jay
    Jay Member
    Options
     What does the image above produce?
  • Jay
    Jay Member
    Options
    I am asking explicitly how to set up this function page to create both scenarios
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     it should adapt to the API call. If it recognizes the parameters are query parameters it will append them accordingly to the URL. If they are part of a request body they would be treated as so.

    Give it a try and you should see in the request object results
  • Jay
    Jay Member
    Options
    

    Adapt how? based on if it is POST or GET?

    I am getting a bad request and can't figure out why:
    [image.png]
    Looking at the params above it makes me thing it put them in the url instead of the body, or constructed the body wrong. 

    I am trying to create a Post request with this body:
    {
    "grant_type": "refresh_token",
    "refresh_token": "502416488029-kz3mDNjrWYInCnwuzZ-UkFOt6HhOdg"
    }or at least i think that is what I am supposed to send. 

    Maybe the problem is with the `%5B0%5D` between refresh_token and the "=" ? Not sure what that is 
  • Jay
    Jay Member
    Options
    Solved. My var user_refresh_token was somehow an array with one string instead of just a string.
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     sounds like you may have been using a query all records and returning a list type. This can be changed to single (or aggregate, count, existence) 
  • Jay
    Jay Member
    Options

    isn’t query all records overkill when I know exactly which single record I need?
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     yup! Get Record is a shortcut if you know exactly what you are looking for
  • Jay
    Jay Member
    Options
     Oh i see, i was using query all, got 1 result back but used that result to get item from user record, which resulted in an array...? Hmm.

    Here is why i used query all records, because Get single record doesn't offer EVALS. Is there a way to do that with get record?

    I basically need a "calculated value":
    [image.png]
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     - No Get Record is just a shortcut to lookup and individual record based on a single field. Once you introduce multiple parameters, joins, and advanced topics such as an eval you will want to use the Query All Records function because it is flexible and handles all that logic.

    As I mentioned, Query All Records is capable of different return types: List, Single, Aggregates, etc. By default it is set to returns a list (query ALL records). Just because you might be returning 1 record does not mean you will be returning a single object return type. A list of 1 is still a list. If you want to return a single object type then you must change the return type from List to Single always.
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    [CleanShot 2021-12-21 at 12.36.34.png]
  • Jay
    Jay Member
    Options

    ok cool. I just worry about querying the entire table when I know I only need one result, efficiency wise
  • Dimitris Giakoumis
    Options
    I know this has been answered but I'm still a bit confused on the functionality described. Michael states " The params section of the external API request is flexible to handle both Query Parameters and JSON request body.  "
    How do we select which mode it's in? What do we press to make it append to the URL or the body?

    (I'm 70% sure I'm staring at it and not seeing it .. )
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
     you don't need to do anything to differentiate when using the params section - it should append to the URL accordingly
  • Jacksonm93
    Jacksonm93 Member
    Options

    I still don't understand how to send a record to an external API in the body.
    There is no clear explanation anywhere.

    Thanks in advance

  • Jay
    Jay Member
    Options

    You have to construct the json i think, there are some examples here: https://docs.xano.com/working-with-data/functions/external-api-request#building-an-external-api-request-two-ways