Curl with a config file


TutorialI use curl almost daily to test my APIs, and Identity server. I am used to working comandline so find this method just works for me. I have been having a problem with some of my requests. The thing is a lot of the requests I make require that I be authenticated to make them. This means that i need to add a access token to the request header. As you can probably guess this leads to some crazy large requests. The the second part of the problem is that every hour i need to rebuild these requests when the access token expires and I fetch a new one.

[wp_ad_camp_3]

The crazy long Request

curl -X GET –header ‘Accept: application/json’ “https://my.api.com/Api/customer/975/Partner/Search?listOptions.pageSize=20&filter.partnerType=xena_partnertype_person&filter.phoneNumber=2020” -H “Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjI2OTAzNzc0NjYyNzc4RDM1MzE2QUUwQUU1MjIyQUU1REIzM0M0NUEiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJKcEEzZEdZbmVOTlRGcTRLNVNJcTVkc3p4Rm8ifQ.eyJuYmYiOjE1MTIwNDQ1MjQsImV4cCI6MTUxMjA0ODEyNCwiaXNzIjoiaHR0cHM6Ly9sb2dpbi54ZW5hLmJpeiIsImF1ZCI6WyJodHRwczovL2xvZ2luLnhlbmEuYml6L3Jlc291cmNlcyIsInRlc3RhcGkiXSwiY2xpZW50X2lkIjoiMTNmMDQ3MjAtZTljNi00NWZhLTg1MDQtYjYyMWY0YzZmMmNmLmFwcHMueGVuYS5iaXoiLCJzdWIiOiIxOTU4MDIwMzUiLCJhdXRoX3RpbWUiOjE1MTIwNDQ1MjMsImlkcCI6ImxvY2FsIiwibmFtZSI6IlhlbmEgT25ib2FyZGluZyIsInJlc291cmNlX2lkIjoiMTk1ODAyODQzIiwieGVuYV9maXNjYWxfaWQiOiI5ODgxOSIsImZpc2NhbF9uYW1lIjoiWGVuYSBBcFMiLCJwaWN0dXJlX3VybCI6Ii9Db250ZW50L2ltYWdlcy9hdmF0YXItY29tcGFueS14ZW5hLmpwZyIsImUtbWFpbCI6Im9uYm9hcmRpbmdAeGVuYS5iaXoiLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiwidGVzdGFwaSJdLCJhbXIiOlsicGFzc3dvcmQiXX0.R4S0waB5t2v0PgyFtxXcd33S1jPO6GOxhEOgtDrQWKEOHmSIYNdnE28YiRd8PksugXDOkfcNr9egvajYXHPYwCSC_qKVZgXAQNz_2FV6EDkoghlA8RgjAp6Xc7PptxgIA4S3HuxBGRdkzyB5apj5X_ODi5darjEICbM4KgGTvkS1zVvTYnYU4jHDG_Kzs7bSJTUKU-xkEag_j0bSPQLBGDUJurPyWssrxjAW7FS7jNIaSHpQKz2PtpXzzhcaoI-ND5OOtSh7pd4HaUgLsJk7gi9BkMj-MDnC3U3Uv28tv3d_L_PXkUohMYhptURZITbOw5IIIrcfmdo9Emj9-VDwQA”

So i went digging around CURLs website and I found out that curl supports config files.

I created a config file in the same directory that I am running out of.

Cmdline.txt

# Add Bearer token
-H “Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjI2OTAzNzc0NjYyNzc4RDM1MzE2QUUwQUU1MjIyQUU1REIzM0M0NUEiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJKcEEzZEdZbmVOTlRGcTRLNVNJcTVkc3p4Rm8ifQ.eyJuYmYiOjE1MTIwNDQ1MjQsImV4cCI6MTUxMjA0ODEyNCwiaXNzIjoiaHR0cHM6Ly9sb2dpbi54ZW5hLmJpeiIsImF1ZCI6WyJodHRwczovL2xvZ2luLnhlbmEuYml6L3Jlc291cmNlcyIsInRlc3RhcGkiXSwiY2xpZW50X2lkIjoiMTNmMDQ3MjAtZTljNi00NWZhLTg1MDQtYjYyMWY0YzZmMmNmLmFwcHMueGVuYS5iaXoiLCJzdWIiOiIxOTU4MDIwMzUiLCJhdXRoX3RpbWUiOjE1MTIwNDQ1MjMsImlkcCI6ImxvY2FsIiwibmFtZSI6IlhlbmEgT25ib2FyZGluZyIsInJlc291cmNlX2lkIjoiMTk1ODAyODQzIiwieGVuYV9maXNjYWxfaWQiOiI5ODgxOSIsImZpc2NhbF9uYW1lIjoiWGVuYSBBcFMiLCJwaWN0dXJlX3VybCI6Ii9Db250ZW50L2ltYWdlcy9hdmF0YXItY29tcGFueS14ZW5hLmpwZyIsImUtbWFpbCI6Im9uYm9hcmRpbmdAeGVuYS5iaXoiLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiwidGVzdGFwaSJdLCJhbXIiOlsicGFzc3dvcmQiXX0.R4S0waB5t2v0PgyFtxXcd33S1jPO6GOxhEOgtDrQWKEOHmSIYNdnE28YiRd8PksugXDOkfcNr9egvajYXHPYwCSC_qKVZgXAQNz_2FV6EDkoghlA8RgjAp6Xc7PptxgIA4S3HuxBGRdkzyB5apj5X_ODi5darjEICbM4KgGTvkS1zVvTYnYU4jHDG_Kzs7bSJTUKU-xkEag_j0bSPQLBGDUJurPyWssrxjAW7FS7jNIaSHpQKz2PtpXzzhcaoI-ND5OOtSh7pd4HaUgLsJk7gi9BkMj-MDnC3U3Uv28tv3d_L_PXkUohMYhptURZITbOw5IIIrcfmdo9Emj9-VDwQA”
# Header stuff
-X GET –header ‘Accept: application/json’

Run it

[wp_ad_camp_5]
Now i can just run this command line

curl -K cmdline.txt “https://my.api.com/Api/customer/975/Partner/Search?listOptions.pageSize=20&filter.partnerType=xena_partnertype_person&filter.phoneNumber=2020”

Note: -k appears to need to be first, putting it at the end of the line didn’t work.


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.