Home > Blockchain >  How to load a request from file using curl or any other command and issue that request
How to load a request from file using curl or any other command and issue that request

Time:06-09

Is there any method available to load the requests from file using curl or any other command and issue that request(GET/POST). Let me explain with example. I have a request stored in file like below -

GET /robots.txt HTTP/1.1
Host: example.com
Cookie: somecookie
User-Agent: any user agent

I want to pass this file in curl so that the curl issues a request to the endpoint. Is there any other method available to do this ?

CodePudding user response:

As already mention in my comment you should split this up in 2 task.

  1. Parse your file(s) into valid curl parameters

if your files are static and have a common structur, you can keep it simple. for example you could use cat with sed and cut to extract information like the host:

$(cat yourfile | sed '2!d' | cut -d ':' -f2)

and also your HTTP method:

$(cat testfile | sed '1!d' | cut -d ' ' -f1)

or your user agent

$(cat testfile | sed '4!d')

now

  1. Combine and script all arguments

for your example you could do this:

curl -X $(cat testfile | sed '1!d' | cut -d ' ' -f1) $(cat testfile | sed '2!d' | cut -d ':' -f2) -H "$(cat testfile | sed '4!d')"

which will result in curl -X GET example.com -H "User-Agent: any user agent" as provided by your input file.

CodePudding user response:

I once created 'h2c' (headers 2 curl) that converts a HTTP request like that into a curl command line reproducing that request.

You can use it online: https://curl.se/h2c/

Or download the open source script from here: https://github.com/curl/h2c

  • Related