'C# Oscova Bot failing on await response.json()

I am trying to work out a C# implementation of the Oscova bot (described in the tutorial); however, when it goes to process the message which is done with an await fetch('...url') and the response is received via await response.json() during the response it is writing the json onto the page ahead of the <!Doctype> tag and causing all further javascript to fail to execute as it throws the SyntaxError: Unexpected token < in JSON at position ... error.

I tried changing the process to:

await fetch('url').then(response => response.json()).then(data => respObj = data);

Then based on other SO articles tried changing the response.json() to response.text(). The response.text() doesn't cause an error but then the rest of the method fails because it sends back a json object and most of the page html text all run together in one string which the method can't process. I've also tried altering the fetch to make sure it posts and accepts a header of 'application/json' which doesn't work either.

The json object that's getting written to the page looks something like this:

{ "success": true, 
    "id": "134566878976413", 
    "user": "user", 
    "response": { 
      "item1": "", 
      "items": [], 
      "item2": "text", 
      "item3": "text", 
      "item4": "text", 
      "item5": "" 
}, 
"item6": 1.0 }

How can I prevent the json from being written to the page or if it's supposed to be able to be written to the page keep it from causing the syntax error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source