API Error Message Format

(Kevin Wildenradt) #1

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

(Régis Hanol) #2

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 :wink:

(Kevin Wildenradt) #3

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: '[...]&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[...]&api_username=notauser
REQUEST onRequestResponse[...]&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[...]&api_username=notauser
REQUEST response end[...]&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[...]&api_username=notauser
REQUEST has body[...]&api_username=notauser 146597
REQUEST emitting complete[...]&api_username=notauser

(Matt Palmer) #4

Can you provide the actual HTTP request that is sent, rather than… whatever that is?

(Blake Erickson) #5

Here is an example where we specify we want json, but because there was an error (the topic couldn’t be found) it returned html.

(Kevin Wildenradt) #6

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.