ValRob
ValRob

Reputation: 2692

Postman request with body Form data to json

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:

enter image description here

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

Thanks!

Upvotes: 13

Views: 86435

Answers (3)

DORRITO
DORRITO

Reputation: 661

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:

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.

Upvotes: 1

ValRob
ValRob

Reputation: 2692

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

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.

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].

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

Thanks: Jean Rostan

Upvotes: 2

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

enter image description here

Upvotes: 6

Related Questions