Creating a new conversation message via API and vb.net

I’m trying to add an update to an item that I have just created using the API in the same format, but I receive a “(500) internal server error” message.

The code I’m using is as follows:

    Dim query As String = "mutation {create_update (item_id: 1320133610, body: ""This update will be added To the item"") { id }}"

    Dim request As WebRequest = WebRequest.Create(apiUrl)
    request.Method = "POST"
    request.Headers.Add("Authorization", apiKey)
    request.ContentType = "application/json"

    Dim requestBody As String = "{""query"": """ & query & """}"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(requestBody)
    request.ContentLength = byteArray.Length

    Using dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
    End Using

    Try
        Dim response As WebResponse = request.GetResponse()
        Using dataStreamResponse As Stream = response.GetResponseStream()
            Using reader As New StreamReader(dataStreamResponse)
                Dim responseFromServer As String = reader.ReadToEnd()
                MsgBox("Job Response: " & responseFromServer)
            End Using
        End Using
        response.Close()
    Catch ex As WebException
        MsgBox(ex.Message)
    End Try

Hello there @mwood and welcome to the community!

I hope you like it here :muscle:

I do not have experience with this language, so I am not sure if there is an issue on how you are constructing the query, but here is your example in cURL syntax so that you can use it as a base example:

curl --location 'https://api.monday.com/v2/' \
--header 'API-Version: 2023-10' \
--header 'Authorization: MYAPIKEY' \
--header 'Content-Type: application/json' \
--data '{"query":"mutation {\n  create_update(\n    item_id: 1320133610\n    body: \"This update will be added To the item\"\n  ) {\n    id\n  }\n}","variables":{}}'

I hope that helps!

Cheers,
Matias

1 Like

Hi, thanks for this.

I have found my issue, but I’m unsure of the solution. My original code works if the body does not contain any spaces.

I also have an issue when creating and item if the item name contains spaces or special characters.

If I surround the text with quotes (“my name”), I get the Internal Server error.

It looks like the issue is down to the way the text is being parsed.

I’ve never used vb.net, but it seems like there’s an issue with the string concatenation. Have you tried the String.format method?


Dim queryString As String = String.format(“mutation {create_update (item_id: {0}, body: “”{1}”“) { id }}”, itemId, bodyString)

Dim requestBody As String = String.format(“{”“query”“: “”{0}”“}”, queryString)

Hi Calum,

Thanks for your suggestion. I’ve Tried this and I get the Internal Server Error for all tests. I think I need to investigate some time into getting vb.net to run a curl command.

1 Like

Thanks for your reply Mark,
Exploring curl sounds like a good idea.

I’m sure you’ve done this one, but just-in-case you haven’t I’ll also suggest to manually verify the query string is correct and working from the API playground, Hoppscotch, or Postman.
Essentially just print the value for query to a log or to the console and then copy/paste to the playground and see if it works.
If your query string is correct and working, then maybe the methods your using for the request/response are interfering.
In that case, I suggest checking the docs on the methods for some more details of what’s happening under-the-hood.
Have you tried using the HttpClient class? It looks like this is what most vb.net devs suggest for POSTing to an API.

Good luck Mark!

1 Like

Hi, thanks for the help. The answer turned out to be quite simple.

In my original code, the query string needed changing slightly:

From:
Dim query As String = “mutation {create_update (item_id: 1320133610, body: ““This update will be added To the item””) { id }}”

To:
Dim query As String = “mutation {create_update (item_id: 1320133610, body: \“This update will be added to the item\”) { id }}”

1 Like