Http Post and Get Using Curl in Linux

curl POST and GET response

It seems you need to send the data in form-url-encoded format with following parameters :

  • cert_text={CERT_CONTENT}
  • decode_type=certificate

You need also X-Requested-With: XMLHttpRequest & Connection: keep-alive headers :

cert_content=$(cat test.crt)
curl 'https://www.sslshopper.com/assets/snippets/sslshopper/ajax/ajax_decode.php' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Connection: keep-alive' \
--data-urlencode "cert_text=$cert_content" \
--data-urlencode "decode_type=certificate"

But for this task, you don't need to call some endpoint to check a certificate, as it's specified in https://www.sslshopper.com/certificate-decoder.html, you can use openssl directly :

openssl x509 -in test.crt -noout -subject -enddate -startdate -issuer -serial

Send request to cURL with post data sourced from a file

You're looking for the --data-binary argument:

curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "@path/to/file"

In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an @ symbol, so curl knows to read from a file.

How to Get HTTP status code with curl post

To get only the http_code you could try:

curl -s -o /dev/null --head -w "%{http_code}" -X POST "https://httpbin.org/post"

you can read/remember it like:

  • -s Silent mode
  • -o Write output to /dev/null
  • --head Get headers
    only
  • -w "%{http_code}" write out the HTTP Status code

To get all the headers from your request try:

curl -I -X POST "https://httpbin.org/post"

It will return something like:

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/json
Date: Thu, 28 Mar 2019 20:12:01 GMT
Server: nginx
Content-Length: 294
Connection: keep-alive

How to post raw body data with curl?

curl's --data will by default send Content-Type: application/x-www-form-urlencoded in the request header. However, when using Postman's raw body mode, Postman sends Content-Type: text/plain in the request header.

So to achieve the same thing as Postman, specify -H "Content-Type: text/plain" for curl:

curl -X POST -H "Content-Type: text/plain" --data "this is raw data" http://78.41.xx.xx:7778/

Note that if you want to watch the full request sent by Postman, you can enable debugging for packed app. Check this link for all instructions. Then you can inspect the app (right-click in Postman) and view all requests sent from Postman in the network tab :

Sample Image

GET and POST request through curl

I read through the GitHub documentation, and at the bottom it describes how to obtain graphical output:

The C&C/Boxer API provides an entry point to generate a PNG image of the DRG of a given text:

$CANDCAPI/drg

The URL accepts the same GET parameter as pipeline and returns a raw PNG file.

Based on this, your GET url should look something like this:

http://gingerbeard.alwaysdata.net/candcapi/proxy.php/drg?semantics=fol

Using the "same" GET parameters means that whatever you passed to the pipeline after the question mark can also be passed to the drg web service.

Modified anwser:

Here's the correct command from the same example:

curl -d 'Every man loves a woman' 'http://gingerbeard.alwaysdata.net/candcapi/drg?semantics=fol'

And for people who are beginners like me, here's to save the png file:

curl -d 'Every man loves a woman' 'http://gingerbeard.alwaysdata.net/candcapi/drg?semantics=fol' >> image.png

curl -GET and -X GET

By default you use curl without explicitly saying which request method to use. If you just pass in a HTTP URL like curl http://example.com it will use GET. If you use -d or -F curl will use POST, -I will cause a HEAD and -T will make it a PUT.

If for whatever reason you're not happy with these default choices that curl does for you, you can override those request methods by specifying -X [WHATEVER]. This way you can for example send a DELETE by doing curl -X DELETE [URL].

It is thus pointless to do curl -X GET [URL] as GET would be used anyway. In the same vein it is pointless to do curl -X POST -d data [URL]... But you can make a fun and somewhat rare request that sends a request-body in a GET request with something like curl -X GET -d data [URL].

Digging deeper

curl -GET (using a single dash) is just wrong for this purpose. That's the equivalent of specifying the -G, -E and -T options and that will do something completely different.

There's also a curl option called --get to not confuse matters with either. It is the long form of -G, which is used to convert data specified with -d into a GET request instead of a POST.

(I subsequently used my own answer here to populate the curl FAQ to cover this.)

Warnings

Modern versions of curl will inform users about this unnecessary and potentially harmful use of -X when verbose mode is enabled (-v) - to make users aware. Further explained and motivated in this blog post.

-G converts a POST + body to a GET + query

You can ask curl to convert a set of -d options and instead of sending them in the request body with POST, put them at the end of the URL's query string and issue a GET, with the use of `-G. Like this:

curl -d name=daniel -d grumpy=yes -G https://example.com/

How to send a header using a HTTP request through a cURL call?

GET:

with JSON:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource

with XML:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST:

For posting data:

curl --data "param1=value1¶m2=value2" http://hostname/resource

For file upload:

curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

curl -X POST -d @filename http://hostname/resource

For logging into a site (auth):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/


Related Topics



Leave a reply



Submit