![](/img/trans.png)
[英]POST request response 422 error {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]}
[英]wordpress: wp_remote_post can't pass body to the fast api, while from api docs and python requests no issue, error msg: value is not a valid dict, 422
背景:我正在嘗試制作一個 fastapi 和 wordpress 插件,它將在本地主機(兩者位於同一台服務器上)中使用 api,我已經開發了 api 並且它通過 swagger ui 和 python 請求庫工作得很好但是不能使用它通過 wp_remote_post 在插件中
api 代碼:
from fastapi import FastAPI
from pydantic import BaseModel
from api.article import add_article, html_format_data
app = FastAPI()
class PostData(BaseModel):
links: list
title: str
@app.post('/create_post/')
def create_post(post_data: PostData):
html_format = html_format_data(post_data.links, post_data.title) # list of dicts
if add_article(post_data.title, html_format):
return {"status": 201, "success": True}
else:
return {"error"}
python 請求碼:
import requests
data = {
"title": "gaming",
"links": [
{
"id": "video id 1",
"thumbnail": "url of thumbnail 1,
"title": "title of video 1"
},
{
"id": "video id 2",
"thumbnail": "url of thumbnail 2 ,
"title": "title of video 2"
}
]
}
res = requests.post('http://127.0.0.1:5000/create_post/', json=data)
print(res.content)
通過 swagger ui 同樣成功:
curl -X 'POST' \
'http://127.0.0.1:5000/create_post/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"links": [
{
"id": "shortand",
"thumbnail": "shortand",
"title": "shortand"
},
{
"id": "shortand",
"thumbnail": "shortand",
"title": "shortand"
}
],
"title": "gaming"
}'
但是當我通過 wordpress 插件執行此操作時,代碼如下:
function create_post( $data, $query ) {
$url = 'http://127.0.0.1:5000/create_post/';
$arguments = array(
'method' => 'POST',
'body' => json_encode(array(
"links" => $data,
"title" => $query
)),
'header' => array(
'Content-Type' => 'application/json',
'accept' => 'application/json'
)
);
$response = wp_remote_post( $url, $arguments );
// echo '<script>console.log(`data: ' . $arguments["body"] . '`)</script>';
echo '<script>console.log(`' . json_encode($response) . '`)</script>';
}
我在控制台中收到此錯誤:
{"headers":{},"body":"{"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dict"}]}","response":{"code":422,"message":"Unprocessable Entity"},"cookies":[],"filename":null,"http_response":{"data":null,"headers":null,"status":null}}
我是 wordpress 和 php 的新手。我確實咨詢了這些解決方案,但似乎沒有任何效果,就像我在 php 中所做的那樣。
FastAPI - “msg”:架構請求中的“值不是有效的字典”
編輯我按照建議做了下面但仍然得到同樣的錯誤,這是 php 代碼:
function create_post( $data, $query ) {
$url = 'http://127.0.0.1:5000/create_post/';
$data_arr = array("links" => $data,"title" => $query);
$data_obj = (object) $data_arr;
$body = json_encode( $data_obj );
$arguments = array(
'method' => 'POST',
'body' => $body,
'header' => array(
'Content-Type' => 'application/json',
'accept' => 'application/json'
)
);
$response = wp_remote_post( $url, $arguments );
echo '<script>console.log(`' . $body . '`)</script>';
echo '<script>console.log(`' . json_encode($response) . '`)</script>';
}
解決了哈哈我的問題是個白痴,我錯過了標頭中的“s”所以它不再需要任何 json 因為它看不到標頭所以它找不到它 json 這就是為什么它給出錯誤不是有效的字典。
從php處理JSON時,我們有時會遇到區分對象和關聯arrays的問題。
在將其提供給json_encode()
之前,嘗試將您的 body 數組轉換為 object,如下所示:
$body = json_encode( (object)
array(
"links" => $data,
"title" => $query
));
print_r ($body); //debugging: examine JSON to pass to web service
$arguments = array(
'method' => 'POST',
'body' => $body,
'header' => array(
'Content-Type' => 'application/json',
'accept' => 'application/json'
)
);
$response = wp_remote_post( $url, $arguments );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.