N
N
Nikita Gimon2019-03-26 01:56:58
API
Nikita Gimon, 2019-03-26 01:56:58

How to execute a POST request with badi and header parameters?

Please help me complete a post request to get json

constructor(){
    super();
    this.state = {
      assets: []
    }
  } 

componentDidMount() {
    fetch('3.xxx.xx.xxx:8080/assets', {
      method: 'post',
      headers: "Content-Type: application/x-www-form-urlencoded",
      body: "key=ecmdecmedmjrjekrkl"
    })
    .then(assets => this.setState({assets}, () => console.log('Fetched!')))
  }

Error:
Unhandled Rejection (TypeError): Failed to execute 'fetch' on 'Window': The provided value is not of type

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-03-26
@prod357

const form = new FormData();
form.append('key', 'ecmdecmedmjrjekrkl');

fetch('/some-path', {
  method: 'POST',
  headers: "Content-Type: application/x-www-form-urlencoded",
  body: form,
}).then(assets => this.setState({assets}, () => console.log('Fetched!')));

In a good way, your application should interact with the server via the REST API, the data is transmitted in JSON format.
A hardcoded passkey is not the best solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question