Python Forum
[split] API in Python and Postman
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] API in Python and Postman
#11
I would like to be able to 'read in' a text file set out as follows :

{
"Parameters": {
"UserAccessKey": "providedkey",
},
"ValueList": {
"1": "8463850375”,
"2": "4534535788",
"3": "0798742342"
...
...
...
"1223": "87878899"}
}

as he list changes weekly - so the payload needs to be dynamic as such. Even if the ValueList part can be read in :

"1": "8463850375”,
"2": "4534535788",
"3": "0798742342"

is this possible?
Reply
#12
yes, it's possible - just read the values from file into dict, e.g. values_from_file and then

payload = {"Parameters":{"UserAccessKey": "providedkey"},
           "ValueList":values_from_file}
 
where values_from_file is a dict with the values you read from file or created some other way
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
HI,

I now have the following code :

#Specify APIKey
APIKey = 'myKey' 
values_from_file = '{1: 5041122,2: 8003124,3: 7200092}'

  
payload = {"Parameters":{"UserAccessKey": APIKey,             
           "ValueList":values_from_file
           }
It runs but doesn't return any results. I'm guessing I have some format issues in my values_from_file variable.. any ideas?

Whereas this hardcoded version runs with no issues:

#Specify APIKey
APIKey = 'myKey'  
  
payload = {"Parameters":{"UserAccessKey": APIKey,
           "ValueList":{"1": "5041122",
                       "2": "8003124", 
                       "3": "7200092"
                       }
           }
I guess for my own self learning I want to be able to first pass a variable 'list' to the payload and then step onto importing a textfile containing the list.

Any help greatly appreciated.
Reply
#14
1. on line 2 in first snippet values_from_file is a string, it should be dict
values_from_file = {'1':'5041122', '2':'8003124', '3':'7200092'}
this is dict with both keys and values being strings. I don't know what your API expects as keys and values, but it worked like this before

2. in both snippets you miss closing bracket after APIKey
payload = {"Parameters":{"UserAccessKey": APIKey},
           "ValueList":{"1":"5041122",
                       "2":"8003124", 
                       "3":"7200092"
                       }
           }
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#15
HI,

I have now have a text file containing :

{'1':'5041122', '2':'8003124', '3':'7200092'}

What would be the best way to read this in? I have tried the following :

temp = open("values_from_file.txt", "r")
values_from_file = temp.read()  

payload = {"Parameters":{"UserAccessKey": APIKey
                          },
           "ValueList":values_from_file
          }
Again, the process runs successfully but no results are returned. Im guessing there are format issues in my input files. This is the very last piece of the puzzle so any help is appreciated. And thanks so much for your help so far.
Reply
#16
how does the format of your txt file looks like? and I mean exactly - are there headers, how many columns. Post sample few lines from the top in output tags.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
Hi, it is exactly as I posted. No headers, just one single string starting with { an ending in }. Should it contain single or double quotes

{'1':'5041122', '2':'8003124', '3':'7200092'}

or

{"1":"5041122", "2":"8003124", "3":"7200092"}

Thanks again
Reply
#18
It should be double quotes: {"1":"5041122", "2":"8003124", "3":"7200092"} as it will make it easier as it will be valid JSON itself and you can load it with json module. You can even (better) use .json extension in this case, but .txt will also do

values.json
Output:
{"1":"5041122", "2":"8003124", "3":"7200092"}
python script
import requests
import json

#specify url
url = 'https://myurlcheck/api.php'

# this is just for my test - you should comment it out
url = 'http://httpbin.org/post'
APIKey = "providedkey"

with open("values.json", "r") as f: # values_from_file.txt
    values = json.load(f)

 
payload = {"Parameters":{"UserAccessKey":APIKey},
           "ValueList":values}

#Fill in headers
headers = {'content-type': 'application/json'}
 
# Call REST API
response = requests.post(url, data=json.dumps(payload), headers=headers)
# as an alternative you may use
# response = requests.post(url, json=payload, headers=headers)

#Print Response
# better use convininece method response.json() as it is JSON response
print(response.json())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#19
Once again thanks!!! Works perfectly
Reply
#20
HI,

I'd like to format the json returned data as a CSV. If I paste the output into a JSON Validator it correctly displays :

{
"Results": {
"10000": {
"Code1": "9108423",
"Code2": "0",
"Code3": "not available",
"Code4": "Confirmed",
"Code5": "N/A",
"Code6": "N/A"
},
"10008": {
"Code1": "410661",
"Code2": "0",
"Code3": "not available",
"Code4": "Confirmed",
"Code5": "N/A",
"Code6": "N/A"
},
"10009": {
"Code1": "1066228",
"Code2": "0",
"Code3": "available",
"Code4": "Confirmed",
"Code5": "N/A",
"Code6": "7"
}
},
"Override": [],
"Summary": {
"Total": 864,
"Inferrence": 12,
"GlobalUnits": 7206,
"TotalDivereged": 3808,
"ErrorCount": 2,
"ProcessingTimeSeconds": 14.591021
},
"APIMessage": {
"StatusCode": 200,
"Message": "SUCCESS"
}
}

Could a csv be created to display :

Code1,Code2,Code3,Code4,Code5,Code6
9108423,0,not available,Confirmed,N/A,N/A
410661,0,not available,Confirmed,N/A,N/A
1066228,0,available,Confirmed,N/A,7

Thanks again
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  upload image with generated code from Postman does not work magic7598 0 1,695 Nov-30-2019, 10:32 AM
Last Post: magic7598
  API in Python and Postman melo 4 16,655 Aug-29-2018, 12:43 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020