API にリクエストを送るのに パラメーターをつけてリクエストを出したいのですが、余分な文字列
「%22」が入ってしまい、エラーになってしまいます。
python
1def setParameters(s, d=maya.when("1 month ago")): 2 """set Parameters for make usage call to API.""" 3 print(s, d) 4 return {"year": d.year, "month": d.month, "billingDayOnly": True, "statisticName": s} 5 6def getListUsageperContract(contractId, productId, parameters): 7 """measures{?billingDayOnly,fromMonth,fromYear,month,statisticName,toMonth,toYear,year}""" 8 path = f"/billing-center-api/v2/contracts/{contractId}/products/{productId}/measures" 9 data_string = parse.urlencode({p: json.dumps(parameters[p]) for p in parameters}) 10 result = s.get(parse.urljoin(BASEURL, path), params=data_string) 11 return result 12
python
1def setParameters(s, d=maya.when("1 month ago")): 2 """set Parameters for make usage call to API.""" 3 print(s, d) 4 return {"year": d.year, "month": d.month, "billingDayOnly": True, } 5 6parameters = setParameters() 7 8parameters["statisticName"] = s 9
でも同じように余分な「%22」が入ってしまいます。
"statisticName": s を抜いたばいいうまく結果がでるので 該当箇所はここだと特定できているのですが、
ドキュメントのサンプリには該当箇所が 「statisticName=Total+MB」のようになっているので、「%22」
を取り除きたいのですが、どのように直したらいいでしょうか。
最終
python
1def setParameters(s, d=maya.when("1 month ago")): 2 """set Parameters for make usage call to API.""" 3 return {"billingDayOnly": "true", "statisticName": f"{s}","month": d.month, "year": d.year, } 4 5def getListUsageperContract(contractId, productId, parameters): 6 """measures{?billingDayOnly,fromMonth,fromYear,month,statisticName,toMonth,toYear,year}""" 7 path = f"/billing-center-api/v2/contracts/{contractId}/products/{productId}/measures" 8 data_string = parse.urlencode(parameters) 9 result = s.get(parse.urljoin(BASEURL, path), params=data_string) 10 return result 11
parse.urlencode を直接読み込むと"billingDayOnly": True =>?billingDayOnly=True になってしまいエラーになったので 文字列 "true" にするとうまくいきました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/20 02:27