I am trying to send a post request using python script and want to store the response. Below are the two file. Everything is fine when i do the same thing using node.js but when I use a python script instead of node.js it give me this error. Any one know why? not able to find the proper reason why this error pop up.
in request sending first name and last name and in response i will get the full name
Node.js Code (used fastify)
const conn=require('fastify')({
logger:true
})
const PORT=80
const axios = require('axios')
//take first and last name as request and in response return full name
conn.post('/user',async(req,res)=>{
const user=req.body
const fullname=user.first_name user.last_name
console.log(full)
res.json(fullname)
})
const start_server=async()=>{
try
{
await conn.listen(PORT)
console.log(`server start at PORT ${PORT}`)
}
catch(error)
{
conn.log.error(error)
process.exit(1)
}
}
start_server()
my Python script
import requests
import json
API_ENDPOINT = 'http://localhost:80/user'
headers = {'Content-type': 'application/json'}
data = {
"first_name": "jayanta",
"last_name": "lahkar"
}
r = requests.post('http://localhost:80/user', data)
print(r.json())
Error message
{'statusCode': 415, 'code': 'FST_ERR_CTP_INVALID_MEDIA_TYPE', 'error': 'Unsupported Media Type', 'message': 'Unsupported Media Type: application/x-www-form-urlencoded'}
CodePudding user response:
Try:
r = requests.post(url = 'http://localhost:80/user', data = data)
print(r.json())
CodePudding user response:
here is the answer to your error:
axios.post('url', data, {
headers: {
'Content-Type': 'application/json',
}
}