How to specify JSON request body example in Postman Collection - javascript

I'm programmatically creating Postman collections and want to provide a default JSON request body to make requests easier.
I've looked through the spec can can't see how to specify it. Does anyone have any ideas? Could this be done with JavaScript, which I've used to automatically set headers and environment variables.
Here's the Postman Collection spec definition I'm working with, v2.1.0 draft 4:
https://schema.getpostman.com/collection/json/v2.1.0/draft-04/collection.json
https://schema.getpostman.com/
A request is specified by #/definitions/request.
The body is specified as one of the following. JSON isn't listed so raw is typically used. Here's an excerpt which appears shows that raw is a string type but there's no property to include a default value for the request body.
{
"body":{
"oneOf":[
{
"type":"object",
"description":"This field contains the data usually contained in the request body.",
"properties":{
"mode":{
"description":"Postman stores the type of data associated with this request in this field.",
"enum":[
"raw",
"urlencoded",
"formdata",
"file",
"graphql"
]
},
"raw":{
"type":"string"
}
}
}
]
}
}
Is anyone aware of Postman being able to specify an example for the JSON request body with a pre-created example, using the collection directly or via JavaScript?
UPDATE
The following YouTube video shows on the body can be set dynamically with JavaScript using the following.
const body = {
"productId": 1234
};
pm.globals.set("body", JSON.Stringify(body));
https://www.youtube.com/watch?v=hSX7Dcjy000
Using this approach, it seems the next thing to figure out is if the Postman Collection can import and access custom properties, e.g. x-properties, or if there's some other way to load the example content by overloading an existing property. It seems this can be done by loading a lot of environment variables, one for each request. The final step may be to be to automatically load the correct environment variable value into the example body when the user first brings up the endpoint.
Here's more information on a similar topic:
https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#scripting-with-request-data

The link you added is scripting withing postman test and pre request section , not for programmatically creating json.
You can open postman and click the inverted hamburger menu of collection to export the collection json. You can use this as reference.
In the generated json request is defined as : (Only url , method and body part not full )
"method": "DELETE",
"header": [],
"body": {
"mode": "raw",
"raw": "{{requestbody}}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/resource1/resource2",
"host": [
"{{url}}"
],
"path": [
"resource1",
"resource2"
]
}
This is the json created from postman.

Related

Postman - Possible to check Runner input file to dynamically create body?

I'm frequently using Postman to send requests to a Kanban API. However, the data being sent isn't always the same.
For example, the body of the request will always have an ID for the Kanban card to be added to the board, {{External_Card_ID}}, but it won't always have a plannedFinish or a title.
{
"customId": "{{External_Card_ID}}",
"title": "{{Title}}",
"plannedFinish": "{{Due_Date}}"
}
Is it possible to use pre-request scripts in Postman to check the CSV given to the runner to say something like
requestBody = "{
"customId":"{{External_Card_ID}}""
}"
If csvInput contains "Title" column then
append ",title":"{{Title}}" to requestBody
And then set the body to {{requestBody}}?
Solution found: use data.ColumnName

How to call JSONRPC to Odoo API getting data from multiple models in a single request?

I am calling JSONRPC from an application using this code:
const res = await axios.post(server + '/jsonrpc',
{
"jsonrpc": "2.0",
"method": "call",
"params": {
"service": "object",
"method": "execute",
"args": [database, uid, password, model, "search_read", [domain], fields, 0, 0, orderBy],
"id": 1,
}
}
);
When I want to get data from 1 model, it works fine. However, when I want to get data related to this model, I currently have to do this call again to the destination model with the domain according to the related origin data. For example, I know the Sale Order id and I want to get Stock Pickings that belong to that specific Sale Order, I need to call this 3 times. Not to mention that I need many more other related fields, which requires the application to send multiple requests to the server.
I think there must be a way to put the models and domains together to send the request only once so that it is most efficient like any other query languages and tools can do.
The question is how to do it?

Use the YouTube API to check if a video is embeddable

I'm trying to figure out if a YouTube video is embeddable using the YouTube Data API v3, from answers to similar questions I noticed the status.embeddable property of videos, for a request like this:
https://www.googleapis.com/youtube/v3/videos?id=63flkf3S1bE&part=contentDetails,status&key={MY_API_KEY}
The response is the following
{
"kind": "youtube#videoListResponse",
"etag": "\"ksCrgYQhtFrXgbHAhi9Fo5t0C2I/ctZQYtBcOuMdnQXh8-Fv1EbS_VA\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"ksCrgYQhtFrXgbHAhi9Fo5t0C2I/Cd8aGZD09NPuGYNumIEozZs2S90\"",
"id": "63flkf3S1bE",
"contentDetails": {
"duration": "PT8M23S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": false,
"projection": "rectangular"
},
"status": {
"uploadStatus": "processed",
"privacyStatus": "public",
"license": "youtube",
"embeddable": true,
"publicStatsViewable": true,
"madeForKids": false
}
}
]
}
The embeddable parameter under status is returned as true, HOWEVER this video is not actually embeddable, as can be seen here.
When actually embedding the video using the iframe API, there is a more detailed error message as well:
Video unavailable
This video contains content from International Olympic Committee, who has blocked it from display on this website or application.
Watch on YouTube
I don't see how it is possible to detect this case from the YouTube Data API - can anyone help out?
Other option is used in this answer:
Here, you can use the following URL:
https://www.youtube.com/get_video_info?video_id=<VIDEO_ID>
Where VIDEO_ID is the YouTube video_id you want retrieve the information.
In this case, once you get the response, you'll see a property called "playabilityStatus.status".
Here is a extract of the response:
"playabilityStatus": {
"status": "UNPLAYABLE",
"reason": "The video is not available",
"errorScreen": {
"playerErrorMessageRenderer": {
"reason": {
"simpleText": "The video is not available"
},
Additional to johnh10's answer, some of the results saw in the YouTube webpage is not always shown/available in the APIs.
I have the answer. The file that outputs from the https://www.youtube.com/get_video_info?video_id=
is nothing more than a standard text file that is URLENCODED.
To see it properly you first have to DECODE it using a URLDECODER and then you have to separate the json part from the URL querystring part. To take a look at the JSON part you can use a json formatted and to look at the URL part you can use PrettyPrint URL.
Once you do this you will notice that the tag you are looking for to validate weather the video is playable or not is the one mentioned by the other user here. It sits on the URL parameter named "player_response", after you DECODE the file you will find it easily. This parameter holds a longer JSON file that has the playability status under playabilityStatus.Status.
To manipulate it on Javascript simply parse this part of the file as a JSON file and access your node of choice, or parse it as a text and search for the playabilityStatus node that must be unencoded if you dont care to decode it (nothing to fear, only some %2D and %7B instead of spaces and curly brackets).
Good luck!
Unfortunately, this 'copyright check' happens directly from the player. This data is not available through the API.

Need to print data from an xml response in postman?This xml is a bit strange one

1.I need to do api testing.
2.Here is the xml response given by the server.
3.I need to extract the data of partID,partBrand,quantityAvailable and store it
in an environment variable's and also print it in console.
4.I'm using Postman tool in chrome and used java
script code to perform some tests.
5.Please provide a solution to extract data from this xml.
<Reply
xmlns="">
<productID>G500</productID>
<ProductVariationInventoryArray>
<ProductVariationInventory>
<partID>B11007514</partID>
<partDescription>g500 BLACK M</partDescription>
<partBrand>Gildan</partBrand>
<quantityAvailable>72105</quantityAvailable>
<attributeColor>BLACK</attributeColor>
<attributeSize>M</attributeSize>
<validTimestamp>2017-11-23T05:31:39.689-05:00</validTimestamp>
</ProductVariationInventory>
</ProductVariationInventoryArray>
</Reply>
Here is the code after converting it to json object using "xml2Json(responsebody)"
{
"Reply": {
"productID": "G500",
"ProductVariationInventoryArray": {
"ProductVariationInventory": {
"partID": "B11007514",
"partDescription": "g500 BLACK M",
"partBrand": "Gildan",
"quantityAvailable": "72105",
"attributeColor": "BLACK",
"attributeSize": "M",
"validTimestamp": "2017-11-23T05:31:39.689-05:00"
}
}
}
}
Now,I need to extract the data of partID,partBrand,quantityAvailable and store it in an environment variable's and also print it in console.

How to add new google spreadsheet and update using javascript

Im able to get the spreadsheet data from the below reference
https://developers.google.com/sheets/quickstart/js#step_3_run_the_sample
but I didn't found any sample for creating or updating spreadsheet from javascript.
How can I create a new spreadsheet and update it using javascript v4 google API.
Create a spreadsheet using Spreadsheet API:
Make an HTTP requests like:
POST https://sheets.googleapis.com/v4/spreadsheets
This POST request comes with a request body. The request body looks something like:
{
"spreadsheetId": string,
"properties": {
object(SpreadsheetProperties)
},
"sheets": [
{
object(Sheet)
}
],
"namedRanges": [
{
object(NamedRange)
}
],
}
Don't forget to enable the following scopes:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/spreadsheets
Try it in the oauth playground.
Updating Spreadsheets:
You'll be making use of batchUpdate. This method lets you update any of the spreadsheet details. Changes are grouped together in a batch so that if one request fails, none of the other (potentially dependent) changes is written. The batchUpdate method works by taking one or more Request objects, each one specifying a single kind of request to perform. There are many different kinds of requests. Here's a breakdown on the types of requests, grouped into different categories.
More of that in Updating Spreadsheets docs.
Format looks like:
POST .../v4/spreadsheets/spreadsheetId:batchUpdate
Request body:
{
"requests": [{
"updateSpreadsheetProperties": {
"properties": {"title": "My New Title"},
"fields": "title"
}
}]
}
Full sample here.

Categories

Resources