swaggerpostmanswagger-uiopenapipostman-mocks

Postman: Generating POST request body from GET response


I generate an API and Collection for my app by applying the steps on the following article: The hidden gem: Postman API and Documentation feature.

You may try by using a test endpoint e.g. https://petstore.swagger.io (user:test, pass:abc123).

Here is an example json body that I am trying to generate:

{
    "name": "{{$randomLoremSentence}}",
    "description": "{{$randomAdjective}}",
    "productUuid": "{{productUuid}}",
    "address": "{{$randomLoremSentence}}"
}

However, I am looking for a practical way for generating json body for Postman requests. Is there a proper way for this? Or do ı have to build each request manually? I think there must a a smarter way. Any idea?


Solution

  • The JSON response body is not created within POSTMAN, it is generated by the response from a web API HTTP request.

    The API method that is executed determines the response.

    Once you have determined the response and it's structure, you can then create the request and test script within a POSTMAN Collection.

    It is easier to manually test each HTTP request with sample inputs then copy that into an existing Collection, then write the test scripts for each test case, template any input parameters into URL query strings or the JSON request body with global or collection scoped variables.

    After you have determined how to parameterize and template each request (and both the Test Script and Pre-request Script), you will then be able to implement the test script to create assertions on the JSON response content using BDD expressions.

    I recommend looking at the POSTMAN documentation at

    https://learning.postman.com/docs/writing-scripts/test-scripts/ https://learning.postman.com/docs/writing-scripts/script-references/test-examples/

    as it shows some really good examples on how to create a basic test, then automate it using JavaScript, Chai BDD language and the POSTMAN Collection Runner.

    This is based on my experience with POSTMAN. I am not aware of any simple way to automate request and test script creation from API Swagger definitions as every API method response could have any number of potential responses based on different inputs, so this (I believe) has to be constructed manually by the tester.