![](/img/trans.png)
[英]take a csv file as input from user using React and send it as a post request using axios
[英]trying to send a POST request using python with a JSON object and print the response into a CSV file
我正在嘗試使用 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'}
嘗試:
r = requests.post(url = 'http://localhost:80/user', headers=headers, data=data)
print(r.json())
在 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);
這是您的錯誤的答案:
axios.post('url', data, {
headers: {
'Content-Type': 'application/json',
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.