I have a C# app that makes a post request to a simple http server created in Python but my request never "finishes" and doesn't progress past the point of making an asynchronous POST request. This is the call I'm making from my client (C# app):
private void sendPost(HttpClientAdaptor client, MyDataObject myDataObject) {
var payload = JsonConvert.SerializeObject(myDataObject);
var content = new StringContent(payload, Encoding.UTF8, "application/json");
try {
if (client.isDisposed) {
return;
}
var response = client?.PostAsync(ApiEndpoint, content); // this hangs forever
And my http server written in python:
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
from io import BytesIO
class S(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b'This is POST request. ')
response.write(b'Received: ')
response.write(body)
self.wfile.write(response.getvalue())
def run(server_class=HTTPServer, handler_class=S, port=5000):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...\n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...\n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
I know the request is making it to my server since I used some print statements to print the payload but my client seemingly never acknowledges the 200 response from my server. I've verified the server is running, I'm not mixing up the port, and a GET request works via a browser.
I suspect something's wrong with my python server such that it's not 'finishing' the transaction and therefore my client doesn't get a response.
As an aside: Is there a more simple approach to spin up an http server for my client (windows app written in C#)? I just need a way to return a 200 status.
You are using an async function and are not awaiting it.
Instead of
try
And change your method signature from
To