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

206
Views
Fetch post with body data coming as empty

Im fetching my current location in client side and I'm using fetch.

if ('geolocation' in navigator) {
            console.log('geolocation available');
            navigator.geolocation.getCurrentPosition(async (position) => {
                console.log(position.coords.latitude, position.coords.longitude);
                let lat = position.coords.latitude;
                let lng = position.coords.longitude;
                document.getElementById('latitude').textContent = lat;
                document.getElementById('longitude').textContent = lng;
                const data = { lat, lng }
                const options = {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    data: JSON.stringify(data)
                };
                console.log(options, "options")
                let response = await fetch('/api', options)
                const datas = await response.json();
                console.log(datas, 'after fetch');
            });
        } else {
            console.log('geolocation not available')
        }

At the server side I'm doing post

const express = require("express");
const app = express();

app.use(express.static('public'));
app.use(express.json({ limit: '1mb' }));
// app.use(express.urlencoded());
app.listen(3000, () => console.log('listening at 3000'))

app.post('/api', (request, response) => {
    console.log('I got a request');
    console.log(request.body,"req");
    response.json({
        status: 'success',
        latitude: request.body.lng,
        longitude: request.body.lat
    })
})

But I'm not getting the data in the request body

listening at 3000
I got a request
{} req

Please help me solve this as its been long I'm failing to debug it.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your console loging body so you should send body

const options = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) };

about 4 years ago · Juan Pablo Isaza Report

0

You probably need body-parser to handle json body

https://www.npmjs.com/package/body-parser

var express = require('express')
var bodyParser = require('body-parser');

var app = express()

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
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!