Wednesday, July 12, 2017

Script to curl request and read data from curl response(API)



7/12/2017:

import csv

import json

import urllib, urllib2

from urllib2 import Request, urlopen, URLError, HTTPError

import subprocess

#E2 in the file name is due to, I just need to make use of test data created for E2

input_file=open('json_curl_request_6_30_E2.csv','r')

output_file=open('json_curl_response_6_30_E2.csv','wt')

Reader1=csv.reader(input_file)

Writer1=csv.writer(output_file)


for row in Reader1:

  #Parameterizing

  var1=str(row[0])

  var2=row[1]



  headers = {'a1-Type': 'a2/json','Accept': '*/*','a3': 'CRDA','a4': 'PHONE','a5': 'US','a6': 'USD', 'c': var1 }

   jsonurl = "<url>"




  jsonapi="{\"request_type_code\":"+" \""+var1+"\""+",\"requested_entities\":[{\"app_annual_revenue_amount\":"+ var2+",\"c2\":"+var1+"}],\"unique_id_type\":"+" \""+unique_id_type+"\""+"}"

 

  #print(jsonapi)

  # double quotes jsonapi(hardcoded) is not working

  # single quote jsonapi(hardcoded) is working

  row[0]=var1

  row[1]=var2

 

  curl1="curl -X POST --header 'c:" +var1' --header ......"

  jsonapireq="'"+jsonapi+"'"

  row[3]=curl1+jsonapireq+" 'https://s'"

 
  try:
   

 req= urllib2.Request(jsonurl,jsonapi,headers)



 resp= urllib2.urlopen(req)

 response = json.loads(resp.read())

 row[20]=response

  except urllib2.HTTPError as err:

      print(err.code)

 #Are we printing error in response.csv file, especially HTTPError

 #As I do not see Internal server error related responses are not logged  in response.csv file

  Writer1.writerow(row)

 




input_file.close()

output_file.close()


-------------- working