Json by python URL?

Asked

Viewed 500 times

0

good afternoon! I have a python script that I need to pass a Json via URL to an application, which validates through the mac address. With several researches, I have been testing the code below, even to have an understanding of what is happening. The reason you’re starting in Python. When running the script does not give any error, or 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.json is this way:

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

Thank you

  • Add the snippet where your script sends the json to the url

1 answer

0

This is not how you open a json file.

First you must open the file, and then use json.load to load its contents. See:

import json

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

That said, it seems to me that you don’t want to upload json to a dictionary or Python list, but send the contents of your file file.json. For this, you will send it as you would send any file: first reading its content, and then sending this 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'}
  • You raised a very good question. Using the dictionary is good practice?

  • Inside this file in the "header" I have to point type a "token" in which the application will validate. And before the data files I have to add this token. Left as follows: { "token" : "token"[{"name": "name", "end": "end", "email": "email", "tel": "tel"}]. In the dictionary format I can create this token variable before the data?

  • @vfernando think you’re confusing things a little bit. The dictionary is a basic data structure in Python. When it does headers = {..}, for example, you are already creating a dictionary. I would recommend that you train some of these concepts by making more basic programs until you get the hang of things.

  • Hello Pedro, success in sending the data as directed. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.