Next JS -fetch API with POST method

0 votes
9,755 views
added Mar 1, 2019 in Next JS by LC Marshal Captain (25,790 points)
import React from 'react';
import PropTypes from 'prop-types';
import fetch from 'isomorphic-unfetch';
import { URLSearchParams } from 'url';
import Layout from '../components/template/Layout';

global.URLSearchParams = URLSearchParams;

const Test = () => (
  <Layout classProp="frontpage">
    {/* <FrontMainComponent data={JSONdata} /> */}
  </Layout>
);

Test.getInitialProps = async () => {
  const formData = new URLSearchParams();
  formData.append('property_id', 1000168);
  formData.append('type', 'm');
  formData.append('field_prop_listing_type', 'sale');
  const res = await fetch('https://jsondummy.com/posts', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: formData,
  });

  const data = await res.json();
  console.log(data);
};

export default Test;

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...