Consume rest service

1

I am trying to consume a rest service but at the moment of doing it, it sends me an error.

I leave below the code I am occupying.

import http.client


metadatos = {a:1, b:2, c:3}


conn = http.client.HTTPConnection("10.133.xxx.xxx")
        conn.request("PUT", "/rest", metadatos)
        resp = conn.getresponse()
        print(resp.status, resp.reason)

If you make the connection but at the time of doing the PUT is where I get the error

The error you send me is the following

  

Traceback (most recent call last): File "C: \ Program   Files \ JetBrains \ PyCharm Community Edition   2017.1.4 \ helpers \ pydev \ pydevd.py ", line 1591, in       globals = debugger.run (setup ['file'], None, None, is_module) File "C: \ Program Files \ JetBrains \ PyCharm Community Edition   2017.1.4 \ helpers \ pydev \ pydevd.py ", line 1018, in run       pydev_imports.execfile (file, globals, locals) # execute the script File "C: \ Program Files \ JetBrains \ PyCharm Community Edition   2017.1.4 \ helpers \ pydev_pydev_imps_pydev_execfile.py ", line 18, in execfile       exec (compile (contents + "\ n", file, 'exec'), glob, loc) File "C: /Users/mxe01508121A/PycharmProjects/PublicadorFirmaAutografaDigital/PublicadorFAD.py",   line 73, in       conn.request ("PUT", "/ rest", metadata) File "C: \ Users \ mxe01508121A \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ http \ client.py",   line 1239, in request       self._send_request (method, url, body, headers, encode_chunked) File   "C: \ Users \ mxe01508121A \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ http \ client.py",   line 1285, in _send_request       self.endheaders (body, encode_chunked = encode_chunked) File "C: \ Users \ mxe01508121A \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ http \ client.py",   line 1234, in endheaders       self._send_output (message_body, encode_chunked = encode_chunked) File   "C: \ Users \ mxe01508121A \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ http \ client.py",   line 1064, in _send_output       + b '\ r \ n' TypeError: can not concate bytes to str

I hope you can help me.

Greetings!

    
asked by Memo 13.09.2017 в 18:35
source

2 answers

0

The most informative part is Errno 11004] getaddrinfo failed and basically it is informing you that it can not resolve the address set in hcpConn2 , the reasons can be multiple:

  • The address does not exist or is incorrectly configured (eventually test by IP)
  • The port is closed
  • You use a proxy and it is not working
  • You do not have an internet connection
  • etc. etc.
answered by 13.09.2017 в 19:06
0

As I mentioned at the beginning, before editing the question, you can not include the prefix of the protocol in the address. By using http.client.HTTPConnection it is already understood that the protocol is http . Therefore, "http://10.133.xxx.xxx/..." should not be used, but "10.133.xxx.xxx/..." .

Regarding the current error, first the dictionary is not valid, in any case it should be something like: {"a":1, "b":2, "c":3} , second, you can not cheerfully pass a map object (a dictionary) to the argument body of http.client.HTTPConnection.request .

We do not know anything about how you treat that PUT on the server, which does not make things easier. However, you can use urllib.parse.urlencode to convert the dictionary into a "url-encoded" string:

import http.client
import urllib.parse


metadatos = urllib.parse.urlencode({"a":1, "b":2, "c":3})
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}

conn = http.client.HTTPConnection("localhost", 8080)
conn.request("PUT", "/rest", metadatos,  headers)
resp = conn.getresponse()
print(resp.status, resp.reason)

Or use a JSON:

import http.client
import json


metadatos = json.dumps({"a":1, "b":2, "c":3})
headers = {'Content-Type': "application/json",
           'Accept': "application/json"}

conn = http.client.HTTPConnection("localhost", 8080)
conn.request("PUT", "/rest", metadatos,  headers)
resp = conn.getresponse()
print(resp.status, resp.reason)
  

Note 1: Change the host and the port, the codes are just a simplified example. It would be very helpful to know how you treat that request in the server and how to parse the data in order to help you best.

  

Note 2: I recommend you use the great library requests if you are going to work with this assiduously. http.client / http.server are low level modules and more cumbersome to use, although nothing prevents doing so, of course.

    
answered by 13.09.2017 в 21:30