Sending JSON data in FetchAPI Response

I'm currently struggling with creating a Response object and I can't seem to figure out how to pass a JSON body into it.

Do I need to generate a ReadableStream for this purpose? If so, what is the correct way to do it?

I attempted the following method:

const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(JSON.stringify({message: 'Test'}));
  },
});

new Response(stream, {
  status: 304,
  statusText: 'Not Modified',
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
  },
});

Unfortunately, this approach doesn't seem to be effective.

Answer №1

It seems I jumped the gun by posting a question here. Surprisingly, what I needed to do was much easier than expected, without needing a ReadableStream:

new Response(JSON.stringify({message: 'Test'}), {
  status: 418,
  statusText: 'Teapot',
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
  },
});

The error I made was attempting to assign a body to a Response with a null body status.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determine the Normalized Mouse Motion Direction Across a Collection Using Three.js

JS Fiddle: http://jsfiddle.net/Nfw9M/1/ I am currently delving into the world of matrix operations and need assistance with a particular problem related to mouse movement on a 3D object. Inside my THREE.Object3D, there are three cube meshes that can be ro ...

Guide to summing the values in an input box with TypeScript

https://i.stack.imgur.com/ezzVQ.png I am trying to calculate the total value of apple, orange, and mango and display it. Below is the code I have attempted: <div class="row col-12 " ngModelGroup="cntMap"> <div class="form-group col-6"> ...

What is the best way to convert similar JavaScript functions into a reusable function?

Looking to streamline my code by consolidating these 18 functions into one. I've attempted various approaches, but have been unsuccessful due to the nested loops involved. Below are examples of two out of the 18 functions to highlight the variations. ...

Hosting services for forms and JSON data

I have recently completed the development of my website using HTML, CSS3, jQuery, and JavaScript. Like many others, it includes a form for clients to submit their projects via email. After some research, I came across suggestions to use NodeJS for server-s ...

While the Navbar component functions properly under regular circumstances, it experiences difficulties when used in conjunction with getStaticProps

https://i.stack.imgur.com/zmnYu.pngI have been facing an issue while trying to implement getstaticprops on my page. Whenever I try to include my navbar component, the console throws an error stating that the element type is invalid. Interestingly, I am abl ...

Troubleshooting Problems with JSON Parsing in Node.js

After retrieving a JSON file and utilizing NodeJS to parse it, the structure of the JSON file appears as follows: { "id": 5, "x": 9.996, "y": 0.135, "v": { "x1": 0.653, "y1": -0.064 }, "z": 1.4730991609821347 }, { ...

Encountering an npm issue while attempting to execute the command "npx create-expo-app expoApp"

I've been attempting to set up an expo project, but npm is failing to do so. It does create necessary base files like APP.js, but nothing else. Here's the error I encountered: npm ERR! code ENOENT npm ERR! syscall lstat npm ERR! path C:\User ...

No data submitted to .NET CORE Web API Post

I am attempting to map my JSON data to this specific object public class MockPost { public int id {get; set;} public int CardNumber {get; set;} public bool isAccepted {get; set; } This controller handles the request [HttpPost] public async T ...

What is the optimal way to develop an npm module with a predefined name?

I am currently developing a module in nodejs and utilizing NAPI for its creation. Let's assume the name of my module is "hello", so it would be required in JavaScript as require("hello") However, I wish to require it like this require("Name/hello") ...

Is there a way to transmit a value from a page item on the client side to the server side in Oracle Apex without needing to submit the form?

I implemented a JavaScript function to capture the unique ROWIDs of selected rows in the GRID and send them to a specific page item. var gridView = apex.region("emp").call("getCurrentView"), selectedRecords = gridView.getSelectedRec ...

The Ultimate Guide to Automatically Updating JSON File URLs

I am currently working on a project where I need to retrieve data from a URL using the $.getJSON method. I am interested in finding a way to continuously update this data and replace it in the HTML without needing to manually refresh the page. Although I h ...

Update the button text dynamically when clicked without using an identifier or a class

If we take a look at my button in the following code: <input type="button" value="BLUE" name="button_blue" /> My goal is to have the value="BLUE" changed to value="RED" or any other desired value when the button is clicked. ...

How can I gather information from members who have already signed up?

I have a form that submits data to the Angular Firebase database. Once the form is submitted, I want to display the previously submitted data in the form if the user signs in again. Below is my .ts file: import { Component, OnInit } from '@angular/c ...

Load data from a MySQL Database into a DataTable

I am currently in the process of developing a CRUD application. With a large dataset stored in a MySQL database, my intention is to utilize a JQuery DataTable instead of manually creating a table. However, the issue I am facing is that while the table appe ...

Escaping JSON Special Characters in Encoding

I am currently utilizing MariaDB's COLUMN_JSON() function. A known issue (here) highlights that the function correctly escapes double quotes but overlooks other characters that should also be encoded/escaped. To showcase how the JSON column is create ...

"Adjusting Flex Items Dynamically When Resizing the Screen

Hello everyone, I am a new web developer looking to create a calendar using only pure JS. Currently, I have managed to wrap elements on overflow. My question is: how can I efficiently 'destroy' and 'create' new nodes in a responsive ma ...

Utilizing module.exports and ES6 for exporting imports

I am currently facing an issue trying to import a function into a file and then export it from that file. It seems like a fairly straightforward task, but for some reason I am having trouble getting it to work. search_action.js function search_actions() ...

Container-wrapper unable to contain images, overflowing despite using overflow:hidden property

I am currently working on a unique section for a website that includes a .container-wrapper element consisting of text and multiple images. The container is set to have overflow: hidden; in order to prevent any overflow issues. However, the images within t ...

What are the steps for accessing a JSON file in PhoneGap?

Currently, I am working with Phonegap and need help on how to read a JSON file. Can someone provide guidance on this? function readAsText(file) { var reader = new FileReader(); alert("Inside readAstext"); var jsonArray; reader.onloadend = function ...

Issue with logging messages using console.log in Knex migration script

My concern: I am facing an issue where the console.log('tableNobject: ', tableNobject) does not get logged in my knex migration script. I have attempted the following code snippets: //solution A export async function up(knex: Knex) { const ta ...