0

Im using postman to call an API. The use case i have is I need to call the api using the Python-Requests and add error handling and e-mail confirmation. The response i get should be written to a file. Im completely new to python and does not have any expertise. Can someone help?

This is the python Requests code i have

import requests

url = "http://XXXX"

payload = {}
headers= {}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text.encode('utf8'))
jahan
  • 103
  • 4
  • 19
  • What code have you tried which writes to a file? – quamrana Aug 27 '20 at 20:25
  • https://stackoverflow.com/questions/35120899/python-writing-text-to-a-file/35120948 – difurious Aug 27 '20 at 20:28
  • @quamrana , i tried this response = requests.request("GET", url, headers=headers, data = payload) response.raise_for_status() file = open("/u/users/xxxx/Offers.csv", "w") file.write(response.text.encode('utf8')) file.close() – jahan Aug 27 '20 at 20:51
  • Excellent. That seems like it should work! – quamrana Aug 27 '20 at 20:53
  • @quamrana, it works, but the data is all in one line, i need it in a table format so that i can use it to load into a teradata table. Any options? – jahan Aug 27 '20 at 21:07
  • You need to ask a new question where you show the code you have, plus the data response, the output from the ‘write()’ and your desired output . – quamrana Aug 27 '20 at 21:13

2 Answers2

0

You can write it to a file by doing this:

import requests

url = "http://XXXX"

payload = {}
headers= {}

response = requests.request("GET", url, headers=headers, data = payload)

f = open('dir here', 'w')

f.write(response.text.encode('utf8'))
f.close()
0
import requests

url = "http://XXXX"

payload = {}
headers= {}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text.encode('utf8'))


file = open("file_location.csv", 'a')
save = f"{response.text.encode('utf8')}"
file.write(save)
file.close()
hd1
  • 33,938
  • 5
  • 80
  • 91
Kral
  • 189
  • 1
  • 10
  • file = open("file_location.csv", 'a'). Does this mean, if we run the api call daily, it will append to the existing data? – jahan Aug 27 '20 at 20:36
  • Yes. "w" is for overwriting and "a" is for appending. I don't think it newlines. You can do this by '\n' if you would like to. – Kral Aug 27 '20 at 20:38
  • I did this. file = open("/u/users/xxxx/Offers.csv", "w") . I want it overwritten daily, but when i open the csv file in a text editor, i see the data side by side, all of it in one line. The API actually has 5 to 6 fields. what do i need to do if i want to get the fields side by side so that i can convert into an excel and load into a table using fload or tpt? – jahan Aug 27 '20 at 20:45
  • Seperate it with commas. I think excell reads commas as columns and vertical lines as rows. – Kral Aug 27 '20 at 20:54
  • save = f"{response.text.encode('utf8')}," throws an error saying invalid syntax. – jahan Aug 27 '20 at 21:01
  • what is response.text.encode('utf8')? I can't print it. It works with other variables. I think it may be causing a problem. Are you using python 3? – Kral Aug 27 '20 at 21:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220589/discussion-between-jahan-and-kral). – jahan Aug 27 '20 at 21:36