从文件加载数据时格式错误 json
Bad json format when loading data from file
我尝试使用 microsoft graph API 创建 azure ad b2c 用户流:
user_flow.json
{
"id": "Customer",
"userFlowType": "signUpOrSignIn",
"userFlowTypeVersion": 3
}
main.py
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
with open(jsonConfigFile) as data :
payload = (json.load(data))
data.close()
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
{
"error": {
"code": "BadRequest",
"message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",
"innerError": {
"date": "2022-04-13T13:09:00",
"request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"client-request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
} }
但是如果我按如下方式硬编码 json
,它就可以工作了:
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
payload = json.dumps({
"id": "Customer",
"userFlowType": "signUpOrSignIn",
"userFlowTypeVersion": 3
})
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
有人可以解释一下我在从文件加载 json 时做错了什么吗?
json.load
使用 JSON 字符串的 key-value 对创建一个新字典,它 returns 这个新字典。
您可以将数据作为字符串读取并在请求中发送
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
with open(jsonConfigFile) as data :
payload = data.read()
data.close()
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
或调用 json.dumps 加载 JSON 字典。
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
with open(jsonConfigFile) as data :
payload = (json.load(data))
data.close()
r = requests.request("POST", url, headers=headers, data=json.dumps(payload))
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
我尝试使用 microsoft graph API 创建 azure ad b2c 用户流:
user_flow.json
{
"id": "Customer",
"userFlowType": "signUpOrSignIn",
"userFlowTypeVersion": 3
}
main.py
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
with open(jsonConfigFile) as data :
payload = (json.load(data))
data.close()
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
{ "error": { "code": "BadRequest", "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.", "innerError": { "date": "2022-04-13T13:09:00", "request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "client-request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" } } }
但是如果我按如下方式硬编码 json
,它就可以工作了:
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
payload = json.dumps({
"id": "Customer",
"userFlowType": "signUpOrSignIn",
"userFlowTypeVersion": 3
})
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
有人可以解释一下我在从文件加载 json 时做错了什么吗?
json.load
使用 JSON 字符串的 key-value 对创建一个新字典,它 returns 这个新字典。
您可以将数据作为字符串读取并在请求中发送
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
with open(jsonConfigFile) as data :
payload = data.read()
data.close()
r = requests.request("POST", url, headers=headers, data=payload)
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())
或调用 json.dumps 加载 JSON 字典。
def create_user_flow(jsonConfigFile, msgraph_token):
url = "https://graph.microsoft.com/beta/identity/b2cUserFlows/"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + msgraph_token
}
with open(jsonConfigFile) as data :
payload = (json.load(data))
data.close()
r = requests.request("POST", url, headers=headers, data=json.dumps(payload))
return r.json()
print(create_user_flow("user_flow.json",get_graph_token())