0

I have to store some json datas into db via fastapi. i have more than hundred datas and I cant enter it manually so i decided to create one python script and then after i send those datas via script. but while execution i'm getting some errors. i'm new to python please help me out

import requests
import urllib3
import json
import ast
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
API_ENDPOINT = "https://100.70.180.88:33011/ntspapi/mariadb/db_insert"
API_KEY = "VARE56EF4W68V4v4rw68v13s5D1FV86WRE4Va1365v4"
input ={"ntsp_input": {
"table": "NTSP_NF_DETAILS",
"input": {
"server_name": "mss077_1",
"connection": "",
"type": "MGCF",
"auth_type": "",
"ssh_ip": "172.17.15.10",
"ssh_username": "",
"ssh_password": "",
"ssh_key": "",
"ssh_port": "",
"rest_ip": "",
"rest_port": "",
"rest_username": "",
"rest_password": "",
"rest_token_path": "",
"remote_folder": "",
"sftp_username": "",
"sftp_password": "",
"sftp_port": "",
"sftp_key": "",
"server_support": "",
"common_server_connection": "",
"groupname": "",
"unique_identifier": "",
"fqdn": "",
"interface": "[\"any\"]",
"duration": "",
"filter": "",
"network_analyzer": "",
"capture_timeout": 0,
"task_timeout": 0
},
"condition": "None"
}
}
"""changing python dict into json"""
input_data = json.dumps(input)
input_json = ast.literal_eval(input_data)
data = {'api_dev_key': API_KEY,
        'api_option':'paste',
        'api_paste_code': input_json,
        'api_paste_format':'json'
        }
response = requests.post(url=API_ENDPOINT, data = data, verify=False)
print(response.text)
print(response.status_code)

this is my python code.I'm sending JSON input to FASTAPI via python script. but while executing I'm getting some response I mentioned below

  1. {"detail":[{"loc":["body",0],"msg":"Expecting value: line 1 column 1 (char 0)","type":"value_error.jsondecode","ctx":{"msg":"Expecting value","doc":"api_dev_key=VARE56EF4W68V4v4rw68v13s5D1FV86WRE4Va1365v4&api_option=paste&api_paste_code=ntsp_input&api_paste_format=json","pos":0,"lineno":1,"colno":1}}]}
Chris
  • 4,940
  • 2
  • 7
  • 28
Ram
  • 11
  • 3
    Does this answer your question? [How to POST JSON data with Python Requests?](https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests) – Chris Apr 02 '22 at 09:03
  • Try passing the `input_json` as a json rather than a string and pass it in the `json` argument of `requests.post()`. Like this: `response = requests.post(url = API_ENDPOINT, data = data, json = input_data, verify = False)`. Remember to remove the `api_paste_code` key from your `data` dictionary. – Datajack Apr 03 '22 at 06:16

0 Answers0