Wednesday, June 29, 2005

HTTP POST with curl


I keep forgetting how to perform a HTTP POST with curl, so I want to write this down so I don't forget.


Posting a string:


curl -d "String to post" "http://www.domain.com/file.php"


Posting the contents of a file


curl -d @file_to_post.txt "http://www.domain.com/file.php"

13 comments:

  1. thanks for posting.. exactly what I was looking for :)

    ReplyDelete
  2. Thats right, I'm gonna try it out right now and comment how its going

    ReplyDelete
  3. I tried out but with no success, I might have to pass my password and user name to be able to , I guess....
    Does any body knows how to pass password and user name while posting with curl?
    Thanks for any hint!
    andy

    ReplyDelete
  4. curl -u "username:password" http://www.whatever.com/whatever
    - or -
    curl http://username:password@www.whatever.com/whatever
    If it's a site with a self-signed cert and you have to connect over https, you may have to add the --insecure (I think the short version is -k but don't quote me on that) option to keep curl from yelling at you about certificate mismatches.

    ReplyDelete
  5. In some cases, xml-based web service API's require this header in order to work:
    -H "Content-Type: text/xml" . Placed after the data and before the url, of course:

    curl --silent --data-ascii "${myrequest}" -H "Content-Type: text/xml" http://${myxmlservice}

    ReplyDelete
  6. Thank you very much, it was very useful for me :)

    ReplyDelete
  7. Interesting way to not lose track, I have one file were I stick all code related snippets that I might need later, its getting a little large, maybe it is time for a new strategy:)

    ReplyDelete
  8. Unix provides so many ways to do things. wget also has similar functionality in case curl does not fit the bill. Then again you should be looking at sftp too.

    ReplyDelete
  9. Hi,

    Can we use curl to write data into a log file? Curl will be sending the data but what I want is to able to write the data into a logfile on the other end.

    ReplyDelete
  10. I am struggling to POST data read from a remote file (say http:example1.com/mydata.xml) to a serviceendpoint. The following command doesn't work.

    CURL -POST -d @http://example1.com/mydata.xml http://example2.com/serviceendpoint

    Please help

    ReplyDelete
  11. hey everyone, i am new to curl, can anyone tell me where should i put this line code?

    ReplyDelete

Unlocking Seamless Integration: Navigating Unexpected Hubitat Device Queries and VLAN Challenges for a Smoother Home Automation Experience

During my network debugging efforts , I came across an intriguing observation related to the two Hubitat devices on our network. The logs b...