Passing date from Coda in ISO [PACK Parameter]

:wave: Hey there!

Im developing the Canvas Pack and want to pass a due date from Coda to Canvas in a POST request.

BUT - the Canvas Pack only accepts ISO dates.

I want the flow to be easy for the user so they can simply input any sort of Coda date and have it translate on the pack side to an ISO format. Any idea how to do that?

1 Like

Hi @Scott_Collier-Weir - Great question! Date parameters in Coda are passed to the Pack’s execute function as JavaScript Date objects. These objects have a built in method toISOString() that you can use to convert it to thee ISO 8601 format.

1 Like

So just wrap the user input (from the parameter) in toISOString() and call it good?

Also, Im passing the date into a URL query thing - will I also have to use encodeURIcomponent() on it?

1 Like

toISOString() is a method, so you’d add it to the end with a dot:

execute: async function ([mydate], context) {
  let dateAsString = mydate.toISOString();
  // ...
},

I don’t think an ISO date can include any characters that aren’t URL-safe, but it’s usually always a good idea to encode values just to be safe. If you use the helper function coda.withQueryParms() it will both encode the parameters and add them to the URL in one shot.

let url = coda.withQueryParams(baseUrl, {
  startDate: mydate.toISOString(),
});
2 Likes

Can you confirm that these are functionally equaivalent:

Number 1:

let url = www.mysite.com?name=scott&fav[color]=green

Number 2:

let url = coda.withQueryParams(baseUrl, {
   name: "scott",
   fav[color]: "green"
}
1 Like

Yes, although if your key (part on the left of the colon) has special characters in it (like those square brackets) you need to wrap the key in quotes.

1 Like

So like this:

“Fav[color]”: green

Put the quotes outside everything?

1 Like

Can you say a bit more about why you’re using the square brackets in that context? I’m just worried they may not work quite the way you’re expecting (but maybe they will)

2 Likes

The api which I’m working with demands it =[

https://canvas.instructure.com/doc/api/assignments.html#method.assignments_api.create

1 Like

I think @Nick_HE is right, the docs are really bad!

The first thing is that the creation is with a POST and a body and not with query params, and the second is that you may just need to do:

{
  name: "",
  integration_id: "",
  ...
}

instead of

{
  assignment[name]: "",
  assignment[integration_id]: "",
  ...
}

The assignment[integration_id] is just denoting that is part of the assignment but that is all. I maybe wrong and the devs that designed the Canva API really hate standards.

3 Likes

But - I’ve tested it with query parameters as assignment[name]=MathAssignment and that is in fact working

Would that make you think otherwise?

1 Like

POST requests go with a body and not query params, when done properly :sweat_smile:

1 Like

Also it’s just… weird to have square brackets in query parameters like that.

Personally I would pass the parameters in an object in body if it’ll let you do it that way (it should work that way too). But I guess… if it ain’t broke… ?

If it ain’t broke! Its working for me with those weird square brackets so Im keeping it