2017-03-08 12 views
0

私は、URLからイメージを取得してbase64に変換し、POSTリクエストのパラメータとしてAPIサーバーにリクエストを使用して送信する小さなPython 2.xアプリケーションを作成しています。GETが動作し、POSTが行われない

import csv 
import json 
import requests 
import base64 
import getpass 

f = raw_input("Enter CSV filename: ") 
global clientCode 
clientCode = raw_input("Enter customer code: ") 
username = raw_input("Enter username: ") 
password = getpass.getpass("Enter password: ") 
global url 
url = "https://" + clientCode + ".redacted.com/api" 

def getSessionKey(): 
    querystring = {"request":"verifyUser","username":username,"password":password,"clientCode":clientCode} 
    response = requests.request("GET", url, params=querystring, timeout=10) 
    jr = json.loads(response.text) 
    # print(response.text) 
    global sessionKey 
    sessionKey = jr['records'][0]['sessionKey'] 
    errorCode = jr['status']['errorCode'] 

with open(f, 'rb') as myfile: 
    reader = csv.reader(myfile) 
    rownum = 0 
    getSessionKey() 
    for row in reader: 
     productID = row[0] 
     imageURL = row[1] 
     dlimage = requests.get(imageURL, stream=True, timeout=10) 
     encodedImage = base64.encodestring(dlimage.content) 
     imagequery = {'clientCode':clientCode,'sessionKey':sessionKey,'request':'saveProductPicture','productID':productID,'picture':encodedImage} 
     response = requests.post(url, data=imagequery, timeout=10) 
     print response.status_code 
     ir = json.loads(response.text) 
     errorCode = ir['status']['errorCode'] 
     print errorCode 
     rownum = rownum + 1 

を今、私はresponse = requests.get(url, params=imagequery, timeout=10)に応答行を変更した場合、それは動作します:次のように私の確か素人コードがあります。しかし、これはGET要求なので、サーバーは約1kbを超えるイメージに対してHTTP 414エラーをスローします。上記のようにコードを実行すると、APIサーバーはclientCodeパラメータが表示されないことを示すエラーを表示します。そのため、データが表示されないという理由があります。私は間違って何をしていますか?

お手数をおかけしていただきありがとうございます。

+0

サーバがデータに使用するエンコーディングは何ですか?デフォルトでは、リクエストでフォームエンコードされたデータが使用されます。代わりにjson? –

+0

私はform-encodedとjsonの両方を指定するヘッダーを試しましたが、どちらも同じ結果でした。すなわち、サーバーはclientCodeパラメーターを見ることができませんでした。 –

+0

このAPIのドキュメントはありますか?正確なエラーは何ですか? APIを正しく使用しているかどうかわからないときにAPIの使用に関する問題をデバッグするのは難しいですが、 – pvg

答えて

0

依頼が元のように動作していた理由はまだ分かりませんが、代わりにhttplibを使用するようにコードを書き直しています。

関連する問題