Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
This is a React JS Fetch with a failing POST method

I have a simple react application and I am trying to request and post the method and render out the response inside my return div. I am getting a 400 error in my dev tool (network). I have debugged the app so many times. I just can not hit a jackpot. If you run the endpoints in postman using the token attached you will see the response...

import React from "react";

function Checkout() {
  const handleSubmit = () => {
    const token = "TOKEN";
    const data = {
      transactionReference: "string",
      paymentMethod: "CreditCard",
      checkoutOrderUrl: "http://www.test.com/",
      user: {
        name: "string",
        msisdn: "+27610983142",
        email: "test@test.com",
      },
      payementMethodDetail: {
        RedirectUrl: "http://www.test.com",
        PurchaseEventWebhookUrl: "http://www.test.com",
      },
      bundle: [
        {
          productCode: "311",
          amount: 100,
          currencyCode: "ZAR",
          quantity: 1,
        },
      ],
    };
    const requestOptions = {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
      body: data,
    };
    fetch(
      "--------",
      requestOptions
    )
      .then((response) => response.json())
      .then((res) => console.log(res));
  };

  return (
    <div className="App">
      <button type="submit" onClick={handleSubmit}>
        Post Data
      </button>
    </div>
  );
}

export default Checkout;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You are not serializing your object to JSON. You're actually sending [object Object]. Use JSON.stringify for that:

const requestOptions = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${token}`,
  },
  body: JSON.stringify(data), // <-- here!
};
about 4 years ago · Juan Pablo Isaza Report

0

Inside requestOptions you need credentials: 'include', most likely its CORS related issue.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!