1

How to add a given header to an api request example in the postman api document. I am using 'Content-Type': 'application/json' in the headers of postman to test my api requests. However, after generating the postman document from my api collection the curl request example was:

curl --location --request POST 'http://domain/endpoint' \
--data-raw '{"key1": "value1",
"key2": value2}'

I want to add --header 'Content-Type: application/json' to the curl request example in the doc. The request should be:

curl --location --request POST 'http://domain/endpoint' \
--header 'Content-Type: application/json' \
--data-raw '{"key1": "value1",
"key2": value2}'

I found in this link that it is possible: https://www.postman.com/postman/workspace/postman-team-collections/documentation/1372588-81512aa8-f91a-4bcd-a53a-6037780c730e See the picture from the doc: enter image description here

I tried many possible options from pre script to collection general headers without results and it isn't possible to edit the curl request inside the doc after been generated.

Amirov
  • 66
  • 4
  • https://learning.postman.com/docs/publishing-your-api/authoring-your-documentation/#using-examples-in-your-docs Even in the official doc the subject wasn't evoked – Amirov Aug 13 '21 at 21:19
  • `--request POST` should also be removed when `--data-raw` is used. Especially in combination with `--location`... – Daniel Stenberg Aug 13 '21 at 21:25
  • Yes it can be but it works with. The biggest issue still the missing headers in the curl request – Amirov Aug 13 '21 at 21:38
  • It only works with it when you're lucky: https://daniel.haxx.se/blog/2015/09/11/unnecessary-use-of-curl-x/ – Daniel Stenberg Aug 14 '21 at 21:27

2 Answers2

0

The issue occurred because I didn't really manually set the content-type header in postman. The header that I see in Content-Type was in the hidden section of the postman headers so automatically set by postman.

Amirov
  • 66
  • 4
0

When using curl for testing your API, Postman does not help a lot in generating your headers as you said above, but you can still build your own command simply by using different options provided by curl, example:

curl -v \
-H 'myToken: ...Bpq7gwLMBm_WqWdusSiNKI9pVNU9BTtsOerp14920IFktpcyvK...' \
-H 'Content-Type: application/json' \
-X POST \
-d '{"key1": "value1", "key2": value2}' \
http://domain/endpoint

Please note to

  1. use -H option for adding each of your headers
  2. if your payload has a json format, please add the correspondent content-type.
  3. use -X to indicate the type of your Http request
  4. use -d to add your payload

I had some similar problems when using Postman-generated curl commands, and this answer helped me build my own.

Hope this could still help people stuck in similar problems.

hd84335
  • 8,815
  • 5
  • 34
  • 45