Json by the URL in python?

1

Good afternoon! I have a script in python that needs to pass a Json via URL to an application, which validates through the mac address. With a lot of research, I've been testing the code below, even to get an understanding of what's going on. For the sake of starting in Python. When I run the script it does not give any error, or it accuses at least the attempt to pass something invalid in the log.

import json

url = 'http://aplicacao.com.br/recebe'
files = {'file': ('file.json', open('file.json', 'rb'))}
headers = {'content-type': 'application/json'}

My file file.json looks like this:

[{"nome": "nome", "end": "end", "email": " email", "tel": "tel"},
 {"nome": "nome", "end": "end", "email": " email", "tel": "tel"}]

Thank you

    
asked by anonymous 05.07.2018 / 21:43

1 answer

0

This is not how a json file opens.

First you should open the file, and then use json.load to load the contents of it. See:

import json

with open('file.json', 'r') as fd:
    meu_dicionario = fd.load(fd)

That said, it seems to me that you do not want to load json into a dictionary or Python list, but to send the contents of your file.json file. For this, you will send it as you would send any file: first by reading your content, and then by submitting that content:

with open('file.json', 'rb') as fd:
    conteudo_file = fd.read()

url = 'http://aplicacao.com.br/recebe'
files = {'file': ('file.json', conteudo_file)}
headers = {'content-type': 'application/json'}
    
05.07.2018 / 23:06