带有正文表单数据的邮递员请求到 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49676489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:06:45  来源:igfitidea点击:

Postman request with body Form data to json

jsonrestweb-servicespostmanform-data

提问by ValRob

I have a problem with postman...

我有一个邮递员的问题...

For one side, I can make this request with the body in form data. enter image description here

一方面,我可以使用表单数据中的正文发出此请求。 在此处输入图片说明

But, when I try to send the same request with the body in raw(json) I got this:

但是,当我尝试使用 raw(json) 中的正文发送相同的请求时,我得到了这个:

enter image description here

在此处输入图片说明

I am trying to send the data via Angular 5 to a Drupal 8 Backend.

我正在尝试通过 Angular 5 将数据发送到 Drupal 8 后端。

Thanks!

谢谢!

采纳答案by ValRob

It will depend if the backend can receive a JSON format.

这将取决于后端是否可以接收 JSON 格式。

In my case, I am working with the Drupal 8 Module simple Oauth. and the The format for OAuth 2.0 Bearer tokens is actually described in a separate spec, RFC 6750.

就我而言,我正在使用 Drupal 8 Module simple Oauth。OAuth 2.0 Bearer 令牌的格式实际上在单独的规范RFC 6750中进行了描述。

More concrete here

更具体的在这里

The entity-body follows the encoding requirements of the "application/x-www-form-urlencoded" content-type as defined by HTML 4.01 [W3C.REC-html401-19991224].

实体主体遵循 HTML 4.01 [W3C.REC-html401-19991224] 定义的“application/x-www-form-urlencoded”内容类型的编码要求。

So, In my particular case, I will try to send a form-data from angular.

因此,在我的特定情况下,我将尝试从 angular 发送表单数据。

Thanks: Jean Rostan

感谢:让·罗斯坦

回答by Leandro Soriano

Try adding to the Headers the Content-Type: application/json

尝试将内容类型添加到标题中:application/json

enter image description here

在此处输入图片说明

回答by DORRITO

As stated above, the backend may not accept JSON if you are viewing this question. I was having the exact same issue but with a different front end. For an example that worked for me to just get a starting response, you might use:

如上所述,如果您正在查看此问题,后端可能不接受 JSON。我遇到了完全相同的问题,但前端不同。对于一个对我有用的例子只是得到一个开始的响应,你可以使用:

const axios = require("axios");
const querystring = require("querystring");

const authenticate = async () => {

.post((req, res) => {
  let authData = {
    grant_type: "password",
    client_id: "id",
    client_secret: "secret",
    username: "name",
    password: "password"
  };

  const authResponse = await axios.post(
    "http://blah.com/endpoint",
    querystring.stringify(authData)
  );

  return res.send(authResponse.data);
}

authenticate()

This is in a async/await format that excludes a couple setup steps that you may need including try/catch, but you should be able to take the above and attempt to convert it to your needs and get the needed result.

这是一个 async/await 格式,不包括您可能需要的几个设置步骤,包括 try/catch,但您应该能够采用上述方法并尝试将其转换为您的需要并获得所需的结果。