英文:
Can't read data in Postman, but works when using axios
问题
Here are the translated parts of your text:
目前正在构建一个RESTful API,只是为了练习,但我注意到一件事,就是根据我如何读取它,它可能会抛出错误,不返回结果或者实际工作。前端使用React,使用axios库发出请求。后端只是纯粹的PHP。
我有两种读取方式,
-
file_get_contents("php://input")
当我从前端发出请求时,我能够读取表单数据并使用它。但是当我在Postman中发送请求时,它什么也不返回。有人建议使用x-www-form-urlencoded
,所以我尝试了一下,但结果是一样的。还有人建议使用超级全局变量来读取 -
$_POST['item']
在Postman中,它能够轻松读取数据,但如果我使用axios发送请求,它会抛出未定义数组键警告。
我希望能够在构建API时使用Postman进行测试,而不必在将其整合到实际应用程序中时后来更改它。从这一点开始就一无所知,所以任何建议都将很有帮助。如果我做了愚蠢的事情,我表示歉意。
Axios部分:
let fdata = {
email: "testemail",
achternaam: "testachternaam",
};
axios
.post("http://localhost/api/test", fdata)
.then((result) => {
console.log(result.data)
})
PHP部分:
header('Access-Control-Allow-Origin: *');
header("Access-Contro-Allow-Headers: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers,Authorization ,X-Requested-With");
header('Content-Type: application/json; charset=utf-8');
print_r($data json_decode(file_get_contents("php://input"))); // 这在axios中可以工作,但在Postman中无法工作
echo $email = $_POST['email'];
// 在Postman中工作,但在axios中不工作
在Postman中,我使用formdata
和x-www-form-urlencoded
发送的内容完全相同。
英文:
Currently building a restful API just to practice, but one thing I've noticed is that depending on how I read it, it'll either throw errors, give no results or actually work. Frontend is React using the axios library to make requests. Backend is just plain PHP.
I have two ways of reading,
-
file_get_contents("php://input")
When I make a request from the frontend, I'm able to read the formdata and use it.
However when I send in Postman it returns nothing. People recommended usingx-www-form-urlencoded
and so I tried it, but the result is the same. Someone else recommended reading with the superglobals -
$_POST['item']
In postman it's able to read the data no problem, but if I send with axios, it throws an undefinded array key warning.
I'd like to be able to test the api as I build it using Postman without having to change it later on when I incorporate it within the actual app. Clueless from this point on so any suggestions would be helpful. Apologies if I'm doing something stupid
Axios part :
let fdata = {
email: "testemail",
achternaam: "testachternaam",
};
axios
.post("http://localhost/api/test", fdata)
.then((result)=>{
console.log(result.data)
})
PHP Part
header('Access-Control-Allow-Origin: *');
header("Access-Contro-Allow-Headers: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers,Authorization ,X-Requested-With");
header('Content-Type: application/json; charset=utf-8');
print_r($data json_decode(file_get_contents("php://input"))); // this works in with axios, but doesn't work with postman
echo $email = $_POST['email'];
// works in postman, but not with axios
In Postman I send the exact same in formdata
and also
x-www-form-urlencoded
答案1
得分: 1
Axios会自动将您传递给.post()
的数据转换为JSON格式(请参见https://axios-http.com/docs/post_example)。 相比之下,您的PostMan请求会发送form-url-encoded数据(其中键和值以键-值元组形式进行编码,由&
分隔,键和值之间有=
,例如email=testemail&achternaam=testachternaam
)。
这些是不同的格式,PHP以不同的方式读取它们。 json_decode
无法处理非JSON数据,但反过来只有form-encoded数据会自动添加到$_POST
中。
要从Postman发送与您的Axios请求相同的等效数据,请更改格式为“raw”,然后粘贴
{
"email": "testemail",
"achternaam": "testachternaam",
}
在那里,并选择JSON作为格式。
英文:
Axios automatically converts data you pass into .post()
into JSON format (see https://axios-http.com/docs/post_example). By contrast, your PostMan request sends form-url-encoded data (where the keys and values are encoded in key-value tuples separated by &
, with a =
between the key and the value, e.g. email=testemail&achternaam=testachternaam
).
These are different formats, and PHP reads them in different ways. json_decode
can't work on non-JSON data, but conversely only form-encoded data is added to $_POST
automatically.
To send equivalent data from Postman, to be the same as your Axios request, change the format to "raw" and then paste
{
"email": "testemail",
"achternaam": "testachternaam",
}
in there, and select JSON as the format.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论