Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

Next JS -fetch API with POST method

0 votes
148 views
added Mar 1 in Next JS by LC Marshal Captain (23,720 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;

 

Please log in or register to response this reference.

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