2

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'}
4
  • You define headers in your python script but don't use it in your request Commented Apr 25, 2022 at 18:52
  • if i used headers the it give me the below error: {'statusCode': 400, 'error': 'Bad Request', 'message': 'Unexpected token i in JSON at position 1'} Commented Apr 25, 2022 at 18:58
  • You are note actually passing the headers to the function call. Commented Apr 25, 2022 at 19:02
  • if i pass header to function call then error will be : {'statusCode': 400, 'error': 'Bad Request', 'message': 'Unexpected token i in JSON at position 1'} Commented Apr 25, 2022 at 19:32

3 Answers 3

1

Try:

r = requests.post(url = 'http://localhost:80/user', headers=headers, data=data)

print(r.json())
Sign up to request clarification or add additional context in comments.

8 Comments

tried but not resolved::{'statusCode': 415, 'code': 'FST_ERR_CTP_INVALID_MEDIA_TYPE', 'error': 'Unsupported Media Type', 'message': 'Unsupported Media Type: application/x-www-form-urlencoded'}
Yes you need to add the headers to the data variable. The request is not being read as application/json
how can i do that properly. I am kind of learner. Any hint
I modified my answer, try that
same error Unexpected token i in JSON at position 1...
|
1

in the python script:

use json

r = requests.post(url = 'http://localhost:80/user', headers=headers, json=data)

in the node script:

use .send method

const {first_name, last_name} =req.body;
const fullname={first_name, last_name};
res.send(fullname);

1 Comment

don't know how to thank you.. its works. You just save me. thanks a lot
-1

here is the answer to your error:

axios.post('url', data, {
        headers: {
            'Content-Type': 'application/json',
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.