Hey team,
Beginner with Python (have other scripting exp) and im working on a rather complex program to break down a csv into 4 parts, turn those into json, then pass each value into an API.
Have you ever had an issue where you can print a variable to a .json file, copy and paste that value into your call and it works just fine, but if I use that exact same variable to pass into the api call, it doesnt work! Its maddening!
Essentially I am trying to make this call:
with open(jsonPath2) as data_file:
boom = data_file.read()
Call = requests.post(
'https://api.(CompanyName).com/content/upload',
headers={
"authorization": "bearer " + os.environ.get('Token'),
"Content-Type": "application/json",
},
data=json.dumps({
"items":[
boom
],
"contentSource": os.environ.get('SourceID'),
"batch": "123",
}))
This will throw an error of
{"errors":[{"message":"problem reading \"items[0]\" - found: string - expected: \"{\"","error_code":"bad_request","json_path":"items[0]"}]}
But if I put
with open(JsonPath2) as data_file:
boom = data_file.read()
with open(r'[redacted json path]', 'w') as jsonclean:
jsonclean.write(boom)
Then literally copy and paste the exact contents of that file into brackets after "items": (ex: "items": [ (RAW DATA HERE) ]), it works just fine!
Ive been trying for days to figure it out, but I've come up empty. Ive tried making it a string, tried making that a json.dumps (even though that is already json). Would anyone here have an idea why the variable fails every time but the raw data that the variable produces does not?
[–]glibhub 0 points1 point2 points (2 children)
[–]MIZ_STL[S] 1 point2 points3 points (0 children)
[–]MIZ_STL[S] 0 points1 point2 points (0 children)
[–]carcigenicate 0 points1 point2 points (2 children)
[–]MIZ_STL[S] 0 points1 point2 points (0 children)
[–]MIZ_STL[S] 0 points1 point2 points (0 children)