skip to Main Content

I am hitting the API by using payload and in headers giving Content-Type : 'application/json' still in browser in request headers I’m seeing Content-Type : 'text/plain'

Because of that in the backend I am getting payload in Content-Type : 'text/plain'

export const abctrigger = (payload , signal) => {
  Return fetch (API_URL, {
    method: post,
    Signal: signal
    Body: JSON.stringify(payload),
    headers: {
      Content-Type : 'application/json'
    }
  })
    .then((response) => response.text())
    .then((response) => {
      return respon se;
    })
}
 

I want to Change request headers to to Content-Type : 'application/json'

2

Answers


  1. The hyphen ‘-‘ in the property name probably causes the issue.

    Try this:

    headers: {
      "Content-Type": 'application/json'
    }
    

    Instead of this:

    headers: {
      Content-Type : 'application/json'
    }
    

    And check your IDE settings, because with ms-code the later gives an error:

    Parsing error: Unexpected token, expected ","
    
    Login or Signup to reply.
  2. try this:

    export const abctrigger = (payload, signal) => {
      return fetch(API_URL, {
        method: 'POST', // Corrected method casing
        signal: signal, // Corrected comma
        body: JSON.stringify(payload),
        headers: {
          'Content-Type': 'application/json' // Corrected headers format
        }
      })
      .then((response) => response.text())
      .then((response) => {
        return response;
      });
    };
    

    I made 3 changes, exluding the post, that may be a variable, you forgot a comma and to round Content-Type with ”.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search