I have the following code. I'm calling a source API endpoint using GET and am getting a stream of payload events in the response. I'm selecting specific payloads and am then posting them into another API endpoint. The code seems to work but when I evaluate the body in the destination payload response, I see the content in bytestring and it does not seem to be in json format. The content is some analytics data and does not seem to be incomplete; the json passed in definitely has a lot more detail.
CODE
import json
import requests
import get_token
from requests.auth import HTTPBasicAuth
source_streaming_url = 'https://livestream/api/stream/stream1'
destination_streaming_url = 'https://destinationstream/v1/streams/stream1'
def source_api():
headers = {
'x-api-key': <api_key>,
'Authorization': 'Bearer ' + get_token.get_token().get('result').get('access_token'),
}
response = requests.get(source_streaming_url, headers=headers, stream=True)
for line in response.iter_lines():
if line:
yield line
def main():
for line in source_api():
payload = json.loads(line.decode('utf-8'))
evars_main = payload.get('variable1')
if evars_main and evars_main.get('variable2'):
if evars_main.get('variable2').get('myvar'):
secrets = <get_secret>
destination_response = requests.post(
destination_streaming_url,
headers={"Content-Type": "application/json"},
data=payload,
auth=HTTPBasicAuth(secrets.get('user'), secrets.get('secret')),
stream=True
)
print(destination_response.content)
Response content
reportSuite=stream1&exclude=id&exclude=value&exclude=description&pageEvent=id&pageEvent=type&pageEvent=description&pageEvent=linkURL&pageEvent=linkName&timeGMT=1682996441&receivedTimeGMT=1682996441&hitIdHigh=3614207338102325248&hitIdLow=46195734853736065634&mcVisIdHigh=2951833792067086269&mcVisIdLow=367663990508238318&visIdHigh=41978984813332068260&visIdLow=8004941667833356853&visIdType=5&props=prop29&props=prop40&props=prop73&props=prop30&props=prop33&props=prop21&props=site_section&props=prop48&evars=evars&events=event21&events=event89&geoCountry=jpn&geoRegion=13&geoCity=honan&geoZip=168-0072&geoDMA=30001&geoLatitude=35.63&geoLongitude=139.66&connectionType=LAN%2FWifi&topLevelDomain=.net&languageAbbrev=ja&language=Japanese&searchEngine=Microsoft+Bing&bot=%3CNot+a+bot%3E&operatingSystem=Windows+10&browserType=&browser=&javascriptVersion=1.6&monitorWidth=1600&monitorHeight=1600&monitorColorDepth=16+million+%2824-bit%29&carrier=&mobileDeviceType=&mobileDeviceName=&mobileManufacturer=&mobileScreenWidth=0
&mobileScreenHeight=0&mobileCookieSupport=False&mobileColorDepth=&mobileAudioSupport=&mobileVideoSupport=&pageURL=&pageName=&usesPersistentCookie=1&homePage=0&browserHeight=842&browserWidth=1479&javaEnabled=False&ip=<ip>&isErrorPage=False&purchaseId=&referrer=https%3A%2F%2Fwww.dr.com%2F&state=&userAgent=Mozilla%2F5.0+%28Windows+NT+10.0%3B+Win64%3B+x64%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F112.0.0.0+Safari%2F537.36+Edg%2F&plugins=¤cy=USD&hitSource=1&transactionId=&truncated=False&zip=&mcAudiences=values&mcAudiences=delim
To test this, I spun up a flask server and submitted the post there to retrieve the payloads being sent, and I see the same bytestring output as above. Any idea why this is in this format? How can I view the content in json format on the destination server? Here is the code that I used to spin up the flask server.
from flask import Flask, jsonify, request, Response, stream_with_context
import io
import json
app = Flask(__name__)
@app.route('/stream', methods=['POST'])
def stream():
def generate():
stream = io.BytesIO(request.get_data())
while chunk := stream.read(1024):
yield chunk
print(chunk.decode()) # Display the received data to stdout
return app.response_class(stream_with_context(generate()), mimetype='application/octet-stream')
if __name__ == "__main__":
app.run(debug=True)