I’m running into the issue that, when I make a request to an endpoint, which has ‘Content-Type’: ‘text/event-stream’, it works if that endpoint is GET, but not if it is POST.
Here is a reproduction of the POST case: https://stackblitz.com/edit/stackblitz-starters-xdifdohl
Here is a reproduction of the GET case: https://stackblitz.com/edit/stackblitz-starters-myyztr66
Here is how I am making the request:
const response = await fetch('/event', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
});
Here is how I am reading the server sent events:
const reader = response.body ? .getReader();
const decoder = new TextDecoder();
if (!reader) {
throw new Error('Failed to get response reader');
}
// Process the stream
const processStream = async() = >{
try {
while (true) {
const {
value,
done
} = await reader.read();
if (done) {
log('Stream complete');
break;
}
const text = decoder.decode(value, {
stream: true
});
const lines = text.split('n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6); // Remove 'data: ' prefix
try {
if (data.trim()) {
const parsedData = JSON.parse(data);
log(`Parsed SSE data: $ {
JSON.stringify(parsedData)
}`);
}
} catch(error) {
log(`Error parsing SSE data: $ {
error
}`);
}
}
}
}
} catch(error) {
log(`Stream processing error: $ {
error
}`);
} finally {
reader.releaseLock();
}
};
In the POST case, it’s as though processStream is not invoked at all.