2016-02-02 16 views
5

ben aşağıdakileri yapar bir betik oluşturun çalışıyorum aracılığıyla bir JSON Diziyi gönderin:Python POST

  • bir CSV REST API üzerinden bir uzak sunucuya CSV dosyası gönderin
  • dosyası Ayrıştırma

Sahip olduğum kod CSV dosyasını ayrıştırmak ve bir JSON nesnesine dönüştürmek için çalışıyor. Ancak, uzak sunucuya alındığında, yalnızca ilk satır yazılır. Üste |

Soru: EACH ROW için bir SEPARATE http isteği göndermek ve göndermek zorunda mıyım? Bunun çok hantal olabileceğini hissediyorum. CSV dosyamda 10.000'den fazla satır var ve bu günlük bir cronda yayınlanacak.

Soru: Tüm satırları tek bir istekte içe aktarmak için bunu nasıl ayarlayabilirim?

#Requests package for python import requests 
import csv 
import json 
import requests 

#Parse CSV file and convert to JSON 

f = open('example_import_csv.csv', 'rU') 
reader = csv.DictReader(f, fieldnames = ("u_date","u_product","u_serial_number")) 
out = json.dumps([row for row in reader]) 
#Print output confirms that the JSON is formatted properly 
print(">JSON" , out) 

#Set request parameters 
url = 'xxxxx' 
user = 'xxxxxx' 
pwd = 'xxxxxx' 

#Set proper headers 
headers = {"Content-Type":"application/json","Accept":"application/json"} 

#data=out contains the JSON object 
#Problem is only the first row is imported 
response = requests.post(url, auth=(user, pwd), headers=headers ,data=out) 

#Check for HTTP codes other than 200 
if response.status_code != 200: 
    print('Status:', response.status_code, 'Headers:', response.headers) 
    exit() 

#Decode the JSON response into a dictionary and use the data 
print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json()) 


### OUTPUT 

# >JSON [{"u_serial_number": "11", "u_product": "Apples", "u_date": "1/12/15"}, {"u_serial_number": "12", "u_product": "Pears", "u_date": "1/29/15"}, {"u_serial_number": "13", "u_product": "Oranges", "u_date": "1/12/15"}, {"u_serial_number": "14", "u_product": "Blackberries", "u_date": "1/29/15"}, {"u_serial_number": "15", "u_product": "Blueberries", "u_date": "2/5/15"}, {"u_serial_number": "16", "u_product": "Bananas", "u_date": "2/7/15"}, {"u_serial_number": "17", "u_product": "Strawberries", "u_date": "2/7/15"}] 

# Status: 201 Headers: {'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json'} 

DÜZENLEME: Verileri ServiceNow örneğimize gönderiyorum. İşte python betiği şablonunu açıklayan bir wiki makalesi.

http://wiki.servicenow.com/index.php?title=Table_API_Python_Examples#gsc.tab=0

Burada bir şablon olarak kullanılan temel kod bloğu var. Bunun, örneğe göre tek bir veri satırı için çalıştığını, ancak birden çok veri satırı içe aktarırken işe yaramadığını unutmayın.

#Need to install requests package for python 
#sudo easy_install requests 
import requests 

# Set the request parameters 
url = 'https://myinstance.service-now.com/api/now/table/incident' 
user = 'xxxxxxx' 
pwd = 'xxxxxxx' 

# Set proper headers 
headers = {"Content-Type":"application/json","Accept":"application/json"} 

# Do the HTTP request 
response = requests.post(url, auth=(user, pwd), headers=headers ,data='{"short_description":"Test"}') 

# Check for HTTP codes other than 200 
if response.status_code != 201: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json()) 
    exit() 
+3

Şüphesiz, sorun, uzak sunucudaki gönderiyi işlerken ne ile ilgilidir? –

+0

Teşekkürler Daniel. Sanırım burada bir şey var. ServiceNow'da REST API Gezgini'ni (istekleri oluşturmak için bir grafik kullanıcı arabirimi) kullanarak bunu test ettim. Tabii ki, sadece kayıtlardan biri geçti. ServiceNow'da istek başına yalnızca bir kayıt kabul edebileceği bir sınırlama olabileceğini düşünüyorum. Satıcıya özel bölümde yayın yapacağım. – pengz

cevap