Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REST API HOW TO PASS PARAMETER AS BODY

Status
Not open for further replies.

JINESH GANDHI

IS-IT--Management
May 8, 2022
12
0
0
IN
Hello,

Following is my visual foxpro code and i want to pass parameters as body how to do?

loHttp = CreateObject('Msxml2.ServerXMLHTTP.6.0')

loRequest = loHTTP.open('POST',"
loHttp.setRequestHeader("Content-Type", "text/json")
loHttp.SetRequestHeader("username","testeway@mastersindia.co")
loHttp.SetRequestHeader("password","!@#Demo!@#123")
loHttp.SetRequestHeader("client_id","fIXefFyxGNfDWOcCWnj")
loHttp.SetRequestHeader("client_secret","QFd6dZvCGqckabKxTapfZgJc")
loHttp.SetRequestHeader("grant_type","password")

loHttp.Send( )

Above code not working in visual foxpro
and create error in response
{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

But when I am using POSTMAN and put all parameter in BODY section these api is working.
I dont know how to pass parameter as body
kindly guide me
 
The send method has body as its parameter. So prepare the body into a variable lcBody, for example, and pass it in at the line
Code:
loHttp.send(lcBody)

Chriss
 
Hi Jinesh,

The API documentation will specify parameters that are expected/accepted by the Header and parameters that are expected/accepted in the Body of the request. Please verify the documentation. Anyway, when you say that if you pass parameters in body using Postman and the API works, those are expected to be passed in the Body.

For example, if the 'grant_type' is a Body parameter, your input json for Body would be as below and you can have a variable to store like this:

lcJsonBody = '{"grant_type":"password"}'
..and then..
loHttp.send(lcJsonBody)

Suppose you have 2 more parameters to be passed in the Body json, it would be something like this:

lcJsonBody = '{"grant_type":"password", "Element1":"ValueForElement1", "Element2":"ValueForElement2"}'
loHttp.send(lcJsonBody)

The documentation will tell the correct way of passing the parameters. Different APIs will have their own policy in implementing/accepting user values into the API.

A suggestion: I am not sure the values you have provided here are real ones or just some random samples. Anyway, it's always better not to mention your actually Access keys, usernames, passwords etc in public forums not only here but any forum.

Rajesh



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top