When getting errors back from discourse when using the API, most of them are HTML pages. To handle these errors properly, I need to parse the HTML to determine what went wrong. This is slow. Is there a way to request that the error be sent back in JSON or some other minimal application friendly format? I have tried setting the content type header of the request to application/JSON but this does not seem to help
AI-generated summary
API Error Message Format discussion revolves around the issue of error messages being returned in HTML format when using the Discourse API. Kevin_Wildenradt (1) initiated the discussion, expressing difficulties in handling errors due to the HTML format and requested a way to receive error messages in JSON or a more minimal format.
zogstrip (2) asked to see the raw version of the request, suggesting that the issue might be due to a missing parameter. Kevin_Wildenradt (3) provided the request details, which showed that the content-type header was set to application/json.
mpalmer (4) and blake (5) responded, with blake providing an example where the API returns HTML instead of JSON when an error occurs. Kevin_Wildenradt (6) concluded that it’s not possible to specify JSON error messages in the request and suggested that this feature would improve error handling performance and code clarity.
It appears that the current API behavior returns HTML error messages, even when JSON is requested, if an error occurs.
Can you show us the raw version of the requests that you are sending? You might be missing something since we’re always responding in JSON when asked JSON ![]()
Here is a request I am sending to test the scenario where a request is made with a username that does not in fact exist in discourse. I get back a response with an HTML body.
REQUEST { uri: 'http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser',
method: 'POST',
useQuerystring: true,
form: 'title=my%20great%20discussion%20topic.&raw=This%20is%20the%20topic%20for%20discussion%20its%20super%20great%203&category=DefComments&unlist_topic=true&archetype=regular&tags%5B%5D=coolTag',
headers: { 'content-type': 'application/json' },
callback: [Function] }
REQUEST make request http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser
REQUEST onRequestResponse http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser500 { 'content-type': 'text/html; charset=utf-8',
'content-length': '146597',
'x-request-id': 'f40207af-b443-4781-a378-49b13c3cc46c',
'x-runtime': '0.274651',
'set-cookie': [ '__profilin=p%3Dt; path=/' ],
connection: 'close',
server: 'thin' }
REQUEST reading response's body
REQUEST finish init function http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser
REQUEST response end http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser500 { 'content-type': 'text/html; charset=utf-8',
'content-length': '146597',
'x-request-id': 'f40207af-b443-4781-a378-49b13c3cc46c',
'x-runtime': '0.274651',
'set-cookie': [ '__profilin=p%3Dt; path=/' ],
connection: 'close',
server: 'thin' }
REQUEST end event http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser
REQUEST has body http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser 146597
REQUEST emitting complete http://10.0.1.15:1234/posts?api_key=[...]&api_username=notauser
Can you provide the actual HTTP request that is sent, rather than… whatever that is?
So there is no way to specify on the request that error messages to be sent back in JSON? Would be a good feature and improve error handling performance and code clarity of any software using the API.
That’s the debug output of the node request module, I’m not sure what other information about the rest request that isn’t included above could be relevant, but if there’s some standard way I’m supposed to capture and display it please enlighten me.
