Keiro
Keiro

Reputation: 155

Struggling with curl and bash

There's a bash script for uploading pastes to a pastebin server you own and run, specifically this:

haste() { a=$(cat); curl -X POST -s -d "$a" http://example.com/documents | awk -F '"' '{print "http://example.com/"$4}'; }

However, when attempting to use it in my .bashrc after updating the URL, I get errors similar to the following:

user@domain:~$ curl -X POST -d 'test' example.com /home/user/test.txt https://example.com | awk -F '"' '{print "https://example.com/"$4}'
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100    18  100    14  100     4    127     36 --:--:-- --:--:-- --:--:--   128
chttps://example.com/
url: (3) <url> malformed
100    18  100    14  100     4     33      9 --:--:-- --:--:-- --:--:--    40

I've tried variations of this, like leaving out -X and -s as well as modifying the $4 variable. Frankly, I'm at a loss as to what I'm doing wrong here.

I've also been trying to work it down to where I just do the following:

curl -F file=@/home/user/example.txt example.com 

but I'm not sure if that's even possible.

Is there even a Pastebin service that can do this that I can run?

Edit: Getting closer. Sort of.

user@example:~$ curl  -X POST example.txt -d 'test' https://example.com | awk -F '"' '{print "https://example.com/"$4}'
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     
0curl: (6) Could not resolve host: example.txt
100    18  100    14  100     4     31      8 --:--:-- --:--:-- --:--:--    70
https://example.com/

Seems like maybe I should use the -F bit here for the example.txt?

Upvotes: 1

Views: 1806

Answers (1)

larsks
larsks

Reputation: 311625

Your curl command line syntax in all of the above examples seems problematic. Recall that fundamentally, curl's command line looks like curl [<options>] url1 url2 ... urln. That is, anything that is not an option or an argument to an option is treated as a URL. So if you have:

curl -X POST -d 'test' example.com /home/user/test.txt https://example.com 

You have three non-option arguments: example.com, /home/user/test.txt, and https://example.com. That's why you're seeing curl trying to fetch a URL three times:

The first:

100    18  100    14  100     4    127     36 --:--:-- --:--:-- --:--:--   128

The second:

curl: (3) <url> malformed

The third:

100    18  100    14  100     4     33      9 --:--:-- --:--:-- --:--:--    40

That "malformed url" errors come from trying to fetch /home/user/test.txt:

$ curl /home/user/test.txt
curl: (3) <url> malformed

You don't say which pastebin service you're running in your question, but if it's haste then your curl command should look something like this:

curl --data-binary @/home/user/test.txt -X POST https://hastebin.com/documents

The above sends the file /home/user/test.txt to hastebin. The @<filename> syntax is how you tell curl to read data from a file; see the curl man page for more information. The return value from the above is a JSON document containing the new document key:

{"key":"dudidadoma"}

Upvotes: 2

Related Questions