12
Views
1
Comments
Solved
Possible to turn any text into JSON for REST method?
Question

I'm trying to work with the Quickbase API, and I want to make generic methods (i.e. update record, get record, insert record) and allow the logic to add the necessary fields dynamically based on the need of that particular action. Here's an example request body to update a record in Quickbase:

{
  "to": "bck7gp3q2",
  "data": [
    {
      "3": {
        "value": 1
      },
      "6": {
        "value": "This is my new text"
      },
      "7": {
        "value": 20
      },
      "9": {
        "value": [
          "c",
          "d"
        ]
      },
      "10": {
        "value": false
      },
      "11": {
        "value": "newuser@quickbase.com"
      }
    }
  ],
  "fieldsToReturn": [
    6,
    7,
    9,
    10,
    11
  ]
}

It's the "data" array that's blocking me right now, because the numbers like "3" and "6" and so forth are field IDs on the Quickbase table, and those are the values that I want the logic to be able to manipulate.

The API requires JSON, so I can't send a text body. However, I can build the body like this:

(the above action fails because I am trying to send the body as text/plain and it needs to be JSON)

It seems like OutSystems doesn't allow me to turn that text variable into JSON without first creating the structure for it. However, I can't create the structure because the field ID values will always be changing.

The alternative, and what we have currently setup, is to hardcode each different API call to Quickbase with exactly the fields we want, and create a new API method for each of those actions, on every table we want to do something to/with. That poses an issue because of the way OutSystems handles boolean fields in API bodies because if I want to allow the logic to explicitly make the value false, then I have to make that attribute mandatory, which means I would have to make a new structure for the different actions, which means a different API method for every single action. 

Is there some way to just convert text to JSON without first having to define a structure? In Postman, for example, I can just type out the API request body, and as long as it's valid JSON, it will send and Postman doesn't care if it matches a previously defined entity or not. Am I not thinking about this in the right way? 

2022-11-14 17-25-57
Daniel Johnson
Solution

Found a fix. I declared the content-type header as mandatory and set its value to "application/json", but I left the Request Format property as text/plain and built the request body as shown above. It's working.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.