Escaping Accents via API in Powershell - Encoding Issue?

Hi!

I have automated (or am attempting to create items in a board) using a Powershell script. The item name is based off of data from a SQL Server query.
It so far works perfectly well, except for the fact that accents (é,è,à, etc) cause trouble when sending the webrequest.

$url = “https://api.monday.com/v2/”
$hdr = @{}
$hdr.Add(“Authorization” , “Secret_key!”)
$hdr.Add(“Content-Type”,“application/json; charset=utf-8")

###This mutation will NOT work: item_name = é

$bodytxt ={“query”:"mutation{create_item(board_id:1234,item_name:"é “){id}}”}’
###This mutation will work just fine: item_name = e
$bodytxt ={“query”:"mutation{create_item(board_id:1234,item_name:"é “){id}}”}’

$response = Invoke-WebRequest -Uri $url -Headers $hdr -Method Post -body $bodytxt

What modification is required for this to work? I can use the graphQL playground and make it work just fine with accents, but the webRequest simply ‘bounces’.

Many thanks for any help!

Heyyy @mcd :wave:! Glad you’re part of the community now :slight_smile:

Thanks so much for providing so much context to your question right from the start, that’s amazing! To be transparent with you, I do not have much experience with using PowerShell, but it does seem like a formatting issue when sending this kind of symbols through API. It seems like you might need to play around with formatting the symbol differently in PowerShell itself, at least according to the post I’ve found below with a similar issue:

Could you please take a look and see if the suggested adjustment there does the trick for you?

-Alex

Hi Alex,

Yes I eventually stumbled upon the same article and can confirm that it works just fine.

That said, I am now into a new problem which has to do with changing an item value.
The following argument does NOT work within Powershell even though it works within the API playgroup using the syntax below.

#POWERSHELL (not functional)
$bodytxt = '{"query":"mutation{change_column_value(board_id: 640598638, item_id:' + $a.data.create_item.id + ' ,column_id: "dropdown4", value: "{\"ids\": [1]}"){id}"}'
 ->> value of the $bodytxt variable once executed: 
{"query":"mutation{change_column_value(board_id: 640598638, item_id:665073849 ,column_id: "dropdown4", value: "{\"ids\": [1]}"){id}"}

#API PLAYGROUND (functional)
mutation{change_column_value(board_id: 640598638, item_id:665073849,column_id: "dropdown4", value: "{\"ids\": [1]}"){id}}

What part isn’t escaped properly? The outputted powershell string passed to the webrequest seems identical to the API playground one (to my untrained eye at least)…

Hey @mcd, I recommend using variables to pass json strings so you don’t have to worry about escaping.

Here is an example in powershell that goes through updating column values. It loops trough the items in a board and updates various columns. You should be able to use it to build a valid column values object and pass it in your mutation!

Check it out: https://gist.github.com/yuhgto/17097c7046772b71d1ac788c21898f5c

1 Like

Thanks dipro.

That clarifies ALMOST everything.
My main issue is the syntax for dropdowns in PowerShell:

How do you replicate the {“ids”: [1]} syntax ?

the square brackets seem to need extra help.

Dropdown column
Each dropdown column is a collection of ids and their corresponding label. To update a dropdown column, send the ids of the labels you want to set. If you don’t know the ids of the labels you’re trying to set, you can send the labels text instead. You can only use existing labels, and if the labels you sent doesn’t exist you will get an error containing all the existing labels and their ids.
Raw JSON - use this in your column values variables:
JSON.stringify({
“ids”: [
1
]
});
JSON string - use this in the GraphiQL editor:
“{“ids”:[1]}”

Every column value is a JSON “object” (key-value pairs, or a hash table in Powershell).

For the dropdown column, the key is ids and the value is an array of integers (each integer corresponding to a label for the dropdown).

In the same way that you can serialize hash tables into JSON objects, the Powershell JSON serializer should also support turning an array data structure into a valid JSON string.

This is roughly the procedure you should do to create that column value. I haven’t tested the code, it’s just to demonstrate the process.

$listOfDropdownLabels = 1,2,4 # this is my array
$columnValue = @{"ids" = $listOfDropdownLabels} # this is my column value
$jsonColValue = $columnValue | ConvertTo-Json # this is my JSON column value

You can see a similar process happening on lines 36-39 of the example code (link).

Hope that gets you over the hump!

1 Like