我正在嘗試使用 python 腳本發送發布請求并希望存盤回應。下面是兩個檔案。當我使用 node.js 做同樣的事情時一切都很好,但是當我使用 python 腳本而不是 node.js 時,它給了我這個錯誤。有誰知道為什么?無法找到彈出此錯誤的正確原因。
在發送名字和姓氏的請求中,作為回應,我將得到全名
Node.js 代碼(使用 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()
我的 Python 腳本
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())
錯誤資訊
{'statusCode': 415, 'code': 'FST_ERR_CTP_INVALID_MEDIA_TYPE', 'error': 'Unsupported Media Type', 'message': 'Unsupported Media Type: application/x-www-form-urlencoded'}
uj5u.com熱心網友回復:
嘗試:
r = requests.post(url = 'http://localhost:80/user', headers=headers, data=data)
print(r.json())
uj5u.com熱心網友回復:
在python腳本中:
采用json
r = requests.post(url = 'http://localhost:80/user', headers=headers, json=data)
在節點腳本中:
使用.send方法
const {first_name, last_name} =req.body;
const fullname={first_name, last_name};
res.send(fullname);
uj5u.com熱心網友回復:
這是您的錯誤的答案:
axios.post('url', data, {
headers: {
'Content-Type': 'application/json',
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/463933.html
標籤:javascript Python 节点.js http 斋戒
