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
#1
Hello, I hope its ok for me to add to this thread?

I also have a Postman 'Post' request that is working and I would like to transfer this over to a Python application.

The documentation I have states that the JSON request body be set out as follows :

Output:
{ "Parameters": { "UserAccessKey": "providedkey", }, "ValueList": { "1": "8463850375”, "2": "4534535788", "3": "0798742342"} }
I have created the following python script (in vs2019) :

#import requests library for making REST calls
import requests
import json

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


#Fill in headers
headers = {'content-type': 'application/json'}

#Create payload that will be passed to API for authentication
payload = ""


#Fill in headers
headers = {'content-type': 'application/json'}

#Call REST API
response = requests.post(url, data=json.dumps(payload), headers=headers)

#Print Response
print(response.text)
But I am unsure of how to create the payload correctly... Can anyone help please?

Many thanks
Reply
#2
so what have you tried (post you code in python tags, full traceback in error tags) and ask specific quetions
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
#3
What ever I try and set the payload to, I get the following output error

Output:
{"APIMessage":{"StatusCode":401,"APIMessage":"UNAUTHORIZED: INVALID ACCOUNT KEY"},"Summary":{"ProcessingTimeSeconds":0.013491}} The thread 'MainThread' (0x1) has exited with code 0 (0x0). The program 'python.exe' has exited with code 0 (0x0).
Reply
#4
Still no code... No way to tell what is going on... Don't make it unnecesary difficult to help you. That is assuming your api key is correct and valid
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
#5
Hi thanks for your replies.

Apologies for the disjointed messages..

Ok, this is my code:

#import requests library for making REST calls
import requests
import json

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



#Create payload that will be passed to API for authentication

payload = "{\r\n\t\"Parameters\": {\r\n\t\t\"AccessKey\": \"mykey\",\r\n\t\t\"CheckFlag\": \"Y\",\r\n\t\t\"Items\": {\r\n\t\t\"1\": \"108063780",\r\n\t\t\"2\": \"80637801\",\r\n\t\t\"3\": \"1080662\"\r\n\t}\r\n}\r\n\r\n"


#Fill in headers
headers = {'content-type': 'application/json'}

#Call REST API
response = requests.post(url, data=json.dumps(payload), headers=headers)

#Print Response
print(response.text)
The output is then as follows:

Output:
Output: {"APIMessage":{"StatusCode":401,"APIMessage":"UNAUTHORIZED: INVALID ACCOUNT KEY"},"Summary":{"ProcessingTimeSeconds":0.013491}} The thread 'MainThread' (0x1) has exited with code 0 (0x0). The program 'python.exe' has exited with code 0 (0x0).
The APIKey is correct and works if I use it via postman


Id be very grateful if anyone can help with this.

Many thanks
Reply
#6
your payload (line 12) is a mess with double quotes inside double quotes string and this code cannot possibly run and will produce SyntaxError: unexpected character after line continuation character
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
#7
Hi Buran,

Thanks again for your time.

the documentation stated the JSON Body be set out as follows :

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

Do you have any advice on setting out the payload correctly?
Reply
#8
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'
 
payload = {"Parameters":{"UserAccessKey": "providedkey"},
           "ValueList":{"1": "8463850375",
                        "2": "4534535788",
                        "3": "0798742342"}}

#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())
Note that I use dummy API (look at line 8) in order to show it works. You need to comment line 8. Also note the alternative line 21 (instead of 19)
Output:

Output:
{'args': {}, 'data': '{"Parameters": {"UserAccessKey": "providedkey"}, "ValueList": {"1": "8463850375", "2": "4534535788", "3": "0798742342"}}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '120', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0'}, 'json': {'Parameters': {'UserAccessKey': 'providedkey'}, 'ValueList': {'1': '8463850375', '2': '4534535788', '3': '0798742342'}}, 'origin': '212.50.27.254, 212.50.27.254', 'url': 'https://httpbin.org/post'}

Also, pay attention on "8463850375 - the closing char is not a double quote and it appears like this twice in your posts
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
#9
Thanks - just testing this...

Worked Perfectly! Cant thank you enough!! One last question... If I want to use a txtfile as the payload is this possible/easy to do?

Thanks again
Reply
#10
(Oct-23-2019, 10:27 AM)fioranosnake Wrote: If I want to use a txtfile as the payload is this possible/easy to do?
by txtfile you mean json file or what?
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  upload image with generated code from Postman does not work magic7598 0 1,730 Nov-30-2019, 10:32 AM
Last Post: magic7598
  API in Python and Postman melo 4 16,718 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