无法在 Postman 中读取数据,但在使用 axios 时可以正常工作。

huangapple go评论65阅读模式
英文:

Can't read data in Postman, but works when using axios

问题

Here are the translated parts of your text:

目前正在构建一个RESTful API,只是为了练习,但我注意到一件事,就是根据我如何读取它,它可能会抛出错误,不返回结果或者实际工作。前端使用React,使用axios库发出请求。后端只是纯粹的PHP。

我有两种读取方式,

  1. file_get_contents("php://input")
    当我从前端发出请求时,我能够读取表单数据并使用它。但是当我在Postman中发送请求时,它什么也不返回。有人建议使用x-www-form-urlencoded,所以我尝试了一下,但结果是一样的。还有人建议使用超级全局变量来读取

  2. $_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中,我使用formdatax-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,

  1. 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 using x-www-form-urlencoded and so I tried it, but the result is the same. Someone else recommended reading with the superglobals

  2. $_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

无法在 Postman 中读取数据,但在使用 axios 时可以正常工作。

答案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.

huangapple
  • 本文由 发表于 2023年4月17日 06:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定