No content sent in the request body while implementing fetch

Attempting to send graphql calls from a React component to a PHP server using the fetch method for the first time.

The setup involves React JS on the client-side and Symfony 4 on the server-side. Despite indications that data is being sent in the browser dev tools, all calls result in an empty body on the server side.

Surprisingly, when utilizing graphiql, the requests are successful. I've been trying to replicate the graphiql request structure in React, but the body consistently remains empty.

This is the code snippet showcasing the fetch call:

fetch('/api', {
    'method': 'post',
    'credentials': 'include',
    'headers': {
        'Content-Type': 'application/json',
    },
    'body': JSON.stringify({
        "query": "mutation {UpdateActionStatus(input: {actionId: 61, status: TO_DO}) { id }}",
        "variables": null
    }),
}).then(r => {
    console.log(r.json());

    return r.json();
}).catch(error => {
    console.log('There has been a problem with your fetch operation: ', error.message);
});

Various experiments have been conducted by setting the 'mode' parameter to values like 'cors', 'no-cors', 'same-origin', 'navigate', yet none of these configurations seem to work as expected.

Testing has been done on both Firefox and Chrome browsers, however, without any success.

Even changing the content type to 'application/graphql' did not yield positive results.

Despite reviewing several solutions proposed on platforms like stackoverflow, the issue of empty response bodies persists with no resolution in sight.

An alternative approach involving a more simplistic fetch call was also attempted, unfortunately with similar futile outcomes. It appears there might be an underlying reason preventing the use of fetch altogether, which currently remains unidentified.

fetch('/api', {
    'method': 'post',
    'credentials': 'include',
    'headers': {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    'body': 'test=test',
});

What steps can be taken next? Is there something obvious that is being overlooked in this scenario?

Answer №1

Ensure that the Content-Type and Accept headers are being sent correctly.

'Accept': 'application/json',
'Content-Type': 'application/json', 

Answer №2

My current approach involves utilizing GraphQL along with React, rather than relying on the traditional fetch method, and overall it has proven to be quite effective.

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

Discovering the magic of nesting maps in React-Native without the need for

I am currently developing a react-native app that retrieves its data through an API. I am working on implementing a new feature where the information is grouped by date. The JSON data structure sent by the API looks like this: { "channel_count" ...

XPages XSP Dialog feature experiencing functionality issues

I have a situation with my XPage where all of the extension library dialogs suddenly stopped working. Strangely, I created a new XPage with a test dialog and it works perfectly fine. However, when I try to use the same test dialog on the original XPage, ...

Is there a way to stop Bootstrap from automatically bolding the font?

When testing this simple example without the bootstrap link, everything seems to be working correctly: Hovering over any word changes its color to red, and when hovering away it returns to black. But as soon as the bootstrap link is included, the text bec ...

Issues with $.post function causing HTML to be displayed instead of the intended value

I have a question about sending the value of an HTML element to the server using POST. When I alert the result, I expect to only see the value but I am also getting the HTML content of the PHP file included in my code. Why is this happening? function post ...

Difficulty encountered when saving an ENUM field to MySQL in a web development project with symfony1.4/Doctrine while utilizing a

Currently, I am working on a project that involves processing applications. One crucial component of this project is a grader responsible for assigning a manual grade to each application. These grades are represented by enum values stored in a MySQL field. ...

Choosing specific information in Typescript response

I am encountering an issue with my HTML where it displays undefined(undefined). I have checked the data in the debugger and I suspect that there may be an error in how I am using the select data. Here is a snippet of the code: <div *ngIf="publishIt ...

Is there a way to access hover effect information in Atom editor similar to how it appears in VScode?

Is there a specific plugin required in Atom to display information when hovering over variables, objects, or functions similar to intellisense? VSCode does this automatically, but I am looking for the same functionality in Atom. https://i.stack.imgur.com/ ...

jquery logic for iterating through all elements in a select menu encountering issues

In search of a solution to iterate through all options in a dropdown list using code, comparing each to a variable. When a match is found, I aim to set that particular value as the selected item in the dropdown and then exit the loop. Here's what I&ap ...

Learn how to create a filter to organize food items based on category and subcategory. By selecting a specific category, you can easily access all the related food

To implement a category filter using the provided tables, you can follow these steps: Food Table: Migration $table->bigIncrements('id'); $table->unsignedBigInteger('category_id')->nullable(); $table- ...

Can you guide me on utilizing filter in an Apps Script array to retrieve solely the row containing a particular user ID within the cell?

I am using an Apps Script that retrieves data from four different columns in a spreadsheet. However, it currently fetches all the rows instead of just the row that matches the randomly generated 8-digit user ID. function doGet(req) { var doc = Spreadshe ...

Swapping out a class or method throughout an entire TypeScript project

Currently, I am working on a software project built with TypeScript. This project relies on several third-party libraries that are imported through the package.json file. One such library includes a utility class, utilized by other classes within the same ...

"When a Vuex mutation modifies the state, the computed property fails to accurately represent the changes in the markup

I've encountered a perplexing issue with using a computed property for a textarea value that hasn't been addressed in a while. My setup involves a textarea where user input is updated in Vuex: <textarea ref="inputText" :value="getInputText" ...

Configuring Next.js with Next-Auth, Apollo, and GraphQL

Seeking advice on navigating a next.js front end using Apollo and GraphQL to connect with a PostgreSQL database. Initially, I separated the front and back ends assuming it would be beneficial, but now facing some tradeoffs. Next.js and NextAuth.js seem opt ...

Tips for preventing my component from being duplicated during the development process

I found a helpful guide on creating a JavaScript calendar in React that I am currently following. After implementing the code, I successfully have a functional calendar UI as shown below: // https://medium.com/@nitinpatel_20236/challenge-of-building-a-cal ...

Incorporating HTML elements into a Jade template

Can HTML elements be passed into a jade file? For example, I want to insert text into the p element and nest some content inside the code element within the p element. JSON with string data var news = { one : { title : "Using JSON", body : "Us ...

How to trigger a JavaScript function when the UI-Router state is switched?

Within my Angular App, I am utilizing ui-router to manage navigation and other tasks. In a separate script file, there is a function that looks like this: $(function () { function doSomething(){ if ($('.thisclass').length) { $(' ...

Unable to track user (Mineflayer - Node.js)

Trying to track a player using the mineflayer library in Node.js, but encountering an error within the source code of the library itself. Attempted code: const mineflayer = require('mineflayer'); const { pathfinder, Movements, goals } = require( ...

What is the efficient way to toggle localStorage based on checkbox selection using jquery?

I am looking to efficiently manage localStorage using checkboxes. When a checkbox is checked, I want to add the corresponding value to localStorage, and remove it when unchecked. var selectedModes = new Array(); $('.play_mode').on('click& ...

Is Window.navigator malfunctioning on different browsers in Mac OS?

I'm attempting to access the navigator function in my project to share a specific URL, but I'm facing difficulties accessing it on Mac OS when using browsers other than Safari. Is there a solution to this issue? Below is the function I created f ...

Ways to provide information to an rxjs observer

It appears that most people find observers to be a piece of cake, but I personally struggle with them. I am trying to set up an observable that can receive a number input after it has been created, triggered by pressing a button. However, all the examples ...