REST POST with Form Params in the body of the request giving Error

I am using REST POST to generate Access Token for an API. Below is the requirement
The request must include a Content-Type header with the value of application/x-www-form-urlencoded.
The values for grant_type, client_id and client_secret must be sent via Form Params in the body of the request.

I am using a JSON generator to pass {grant_type, client_id and client_secret} into Rest Post, however I get error message
status code = 400, reason phrase = Bad Request"
“message”: “Invalid grant_type parameter value.”

Please suggest what I am missing here

JSON Generator:
image

Pipeline:
image

How is your REST POST configured?

Any reason you are not passing this information as either part of the Service URL in the REST POST or under Query parameters?

I tried passing the information through Query parameters but that also didn’t work. I am required to pass it in the body(as shown in Postman screenshot)


Postman:

@Akash_Srivastava

Please add all variable in mapper(expires_in,token_type) with same name as you did at postman. Then at HTTP_Entity use “$”. It will work. Let me know if still face issue.

@Supratim,

Thanks for your response. Still got the same error



@Akash_Srivastava
Please click on “=” sign before $ and try.

@Supratim
Still getting the same error


@Akash_Srivastava

I think the issue is setting param. Include those param in mapper which you are passing on Post body. For security token or authentication details pass on header in post snap.

@Supratim

I’m already using mapper to set the values I need to send in body. Please look into my first reply to you!.

@Akash_Srivastava
If possible share pipeline. That might take less time.

@Supratim

Get_API_Key_2018_07_27.slp (12.9 KB)

I think the REST snaps typically send data serialized as JSON and aren’t setup to convert to serialize the entity as ‘application/x-www-form-urlencoded’. So, you’ll need to do the form encoding yourself using an expression like the following:

$.entries().map(elem => elem[0] + "=" + encodeURIComponent(elem[1])).join("&")

Based on your recent updates and the pipeline you attached, if the input document to the REST POST contains only the data you want to send, this expression should work. I’ll break down what it’s doing:

  1. $.entries() – This converts the input document into an array of key/value pairs for each property in the document.
  2. .map(elem => elem[0] + "=" + encodeURIComponent(elem[1])) – For each element in the array produced by the first step, create a string of the form "<key>=<encoded-value>". The encodeURIComponent() takes care of escaping any special characters.
  3. .join("&") – This joins the strings produced for each pair together with an ampersand. The ampersand in URL-encoding is used to separate key/value pairs.

Using this expression as the Entity property for the snap should then produce a string that the REST snap will send to the server.

One thing I noticed in your attached pipeline is that there are some extra quotes around the value for the Content-Type header, those will need to be removed for this to work.

image

@tstack

Thanks for the prompt response. I tried doing as you suggested. Still getting same error. Attached is the pipeline.

Could you please check this pipeline and share your comments

Get_API_Key_2018_07_28.slp (14.5 KB)

The REST Post snap has ‘$’ for the entity. I think you need to use $serialized_body so that you’re sending the serialized string and not a JSON object with a property named ‘serialized_body’.

@tstack
It worked!!

Thanks a lot

hi tstack,

Iam have the same problem. How to use this $serialized_body ? Where should I use this variable (i mean, where i should configure this).

Regards,

Robert

The ‘HTTP Entity’ property in the REST snap. Make it an expression with the value of $serialized_body.

it worked !!! Thanks a lot !