3
respostas

Erro na requisição http pelo python

Estou com este erro quando envio o comando python para requisição http e fazendo curl normalmente a requisição é realizada .podem ajudar ? O erro é este :

 python portal_ativos.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0* Could not resolve host: application
* Closing connection 0
curl: (6) Could not resolve host: application
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3333 (#1)
> PUT /artefato HTTP/1.1
> Host: localhost:3333
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Length: 28
> Content-Type: application/x-www-form-urlencoded
>
} [28 bytes data]
* upload completely sent off: 28 out of 28 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Content-Type: application/json; charset=utf-8
< Content-Length: 115
< ETag: W/"73-51WIRsS/EMTI+evC7B0f8GGJS/c"
< Date: Wed, 26 Feb 2020 21:16:20 GMT
< Connection: keep-alive
<
{ [115 bytes data]
100   143  100   115  100    28    115     28  0:00:01 --:--:--  0:00:01  1833
* Connection #1 to host localhost left intact
{
    "message": " Ativo update !!",
    "ativo": {
        "n": 1,
        "nModified": 0,
        "ok": 1
    }
}Command =>   curl -X PUT -v -H 'Content-Type: application/json' -d '{"nome":"Splunk","versao":"7.3.1"}' http://localhost:3333/artefato

import sys, subprocess, os, shutil, distutils.dir_util, distutils.file_util
from os import listdir, stat, getenv, path, remove
from sys import argv


class PortalAtivos(object):
    def __init__(self):
        self.APP_NAME = "Splunk"
        self.APP_VERSION = "7.3.1"
        self.PORTAL_URL = "http://localhost:3333/artefato"

    def define_update(self, app_name, app_version, portal_url):

        command = "curl -X PUT -v -H 'Content-Type: application/json' -d '{\"nome\"" + ":" + "\"" + app_name + "\"" + "," + "\"versao\"" + ":" + "\"" + app_version + "\"}' " + portal_url
        print("Command =>  ", command)
        subprocess.call(command)

    def update(self):
        self.define_update(self.APP_NAME, self.APP_VERSION, self.PORTAL_URL)

if __name__ == '__main__':
    portalativos = PortalAtivos()
    portalativos.update() 
3 respostas

Ele está mandando o formato, Content-Type: application/x-www-form-urlencoded. Ao invés do formato Content-Type: application/json

Pelo curl no gitbash funciona :

curl -X PUT -v -H 'Content-Type: application/json' -d '{"nome":"Splunk","versao":"7.3.1"}' http://localhost:3333/artefato
* Expire in 0 ms for 6 (transfer 0x273aab0)
* Expire in 1 ms for 1 (transfer 0x273aab0)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Expire in 0 ms for 1 (transfer 0x273aab0)
 Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 149993 ms for 3 (transfer 0x273aab0)
* Expire in 200 ms for 4 (transfer 0x273aab0)
* Connected to localhost (127.0.0.1) port 3333 (#0)
> PUT /artefato HTTP/1.1
> Host: localhost:3333
> User-Agent: curl/7.64.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 34
>
} [34 bytes data]
* upload completely sent off: 34 out of 34 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< Content-Type: application/json; charset=utf-8
< Content-Length: 115
< ETag: W/"73-7EhxcWrYMOugIrzYMnohcZFGtSA"
< Date: Thu, 27 Feb 2020 14:16:49 GMT
< Connection: keep-alive
<
{ [115 bytes data]
100   149  100   115  100    34    485    143 --:--:-- --:--:-- --:--:--   628{
    "message": " Ativo update !!",
    "ativo": {
        "n": 1,
        "nModified": 1,
        "ok": 1
    }
}
* Connection #0 to host localhost left intact

Oi Thiago, como você está? Espero que esteja bem ^-^

Peço perdão pela demora em te retornar. Tente retirar o espaço após os dois pontos, deixe-o da seguinte maneira:

'Content-Type:application/json'

E tente fazer a execução novamente.

Outra alternativa é utilizar a biblioteca de requests ao invés do curl. Nesse site você consegue converter todo o comando curl para request, vale a tentativa.

Qualquer dúvida estou por aqui.