keijack / python-eureka-client

A eureka client written in python. Support registering your python component to Eureka Server, as well as calling remote services by pulling the the Eureka registry.
MIT License
183 stars 43 forks source link

Client API Request With Json Payload #43

Closed takeruadelbert closed 3 years ago

takeruadelbert commented 3 years ago

Hi, recently I am using your library to register my python service to eureka spring cloud. However, I have a problem when I am trying to open api request to another service with json payload. Could you help me how to send data with json?

This is the code which I am using it now:

header = {
    "Content-Type": "application/json"
}
payload = [
   {
      "key1": "value1",
      "key2": "value2"
   },
   { ... }
]
response = eureka_client.do_service(service_name, url_path, data=payload, return_type="json", method="POST", headers=header)
print(response)

The another service which retrieving the payload data wasn't Json type throwing error Broken Pipe.

P.S. I notice that your data param in do_service method receives as data: bytes = None

keijack commented 3 years ago

The data variable in do_service function will be passed directly to the urllib.request, so it accept only bytes. you should use json.dumps(payload).encode() to change your dict to bytes.

takeruadelbert commented 3 years ago

@keijack ah I see, it works now. Thank you so much! cheers

keijack commented 3 years ago

I release a new version, 0.9.2, now dict and str objects will be accepted in do_service function.

takeruadelbert commented 3 years ago

@keijack nice, really appreciate it 👍