I have a server which is supposed to return JSON, I set the request response 'Content-Type' to 'application/json', yet from client side I always get text/html
this is the server printing response headers:
b'Server' = [b'TwistedWeb/22.4.0']
b'Date' = [b'Fri, 08 Jul 2022 18:35:59 GMT']
b'Content-Type' = [b'application/json']
this is the client printing the reply headers:
b'Transfer-Encoding' = b'chunked'
b'Server' = b'TwistedWeb/22.4.0'
b'Date' = b'Fri, 08 Jul 2022 18:35:59 GMT'
b'Content-Type' = b'text/html'
I print headers in the server just before calling request.finish(), is there something happening after that that I should know about ?
this is the server code (simplified):
import json
from twisted.web import server, resource
from twisted.internet import reactor
class Resource(resource.Resource):
def render(self, request):
def return_json(data):
request.write(json.dumps(data, separators=(',', ':')).encode())
request.setHeader(b'content-type', b'application/json')
request.finish()
deferred = someFunctionReturningDeferred()
deferred.addCallback(return_json)
return server.NOT_DONE_YET
site = server.Site(Resource())
reactor.listenTCP(PORT, site)
reactor.run()
What am I doing wrong ?
Many thanks, Paul