POST request with Wget?
Asked Answered
O

1

103

I want to use Wget to upload a picture to a server's test folder using an authentication token, AUTH_1624582364932749DFHDD.

This command doesn't work, I get an "authorization failed" error:

wget --post-file=nature.jpg http://ipadress:8080/v1/AUTH_test/test/ --post-data="AUTH_1624582364932749DFHDD"
Operetta answered 17/7, 2013 at 12:23 Comment(0)
H
130

Wget does not support sending "multipart/form-data" data. --post-file is not for transmitting files as form attachments, it expects data in this form: key=value&otherkey=example. It is actually possible to POST other formats (like JSON) if you send the corresponding header.

--post-data and --post-file work the same way: the only difference is that --post-data allows you to specify the data in the command line, while --post-file allows you to specify the path of the file that contains the data to send.

Here's the documentation:

--post-data=string
--post-file=file

Use POST as the method for all HTTP requests and send the specified data in the request body. --post-data sends string as data, whereas --post-file sends the contents of file. Other than that, they work in exactly the same way. In particular, they both expect content of the form key1=value1&key2=value2, with percent-encoding for special characters; the only difference is that one expects its content as a command-line parameter and the other accepts its content from a file. In particular, --post-file is not for transmitting files as form attachments: those must appear as key=value data (with appropriate percent-coding) just like everything else. Wget does not currently support multipart/form-data for transmitting POST data; only application/x-www-form-urlencoded. Only one of --post-data and --post-file should be specified.

Regarding your authentication token, it should either be provided in the header, in the path of the URL, or in the data itself. This must be indicated somewhere in the documentation of the service you use. In a POST request, as in a GET request, you must specify the data using keys and values. This way the server will be able to receive multiple pieces of information with specific names. It's similar with variables.

Hence, you can't just send a magic token to the server, you also need to specify the name of the key. If the key is "token", then it should be token=YOUR_TOKEN.

wget --post-data 'user=foo&password=bar' http://example.com/auth.php

Also, you should consider using curl if you can because it is easier to send files using it.

Humbertohumble answered 17/7, 2013 at 12:36 Comment(11)
How can i send my auth token in this case ?Operetta
If it were a basic html form, how would you name the input that contains the token? Because that should be: token=AUTH_1624582364932749DFHDDHydrazine
I was going to say it @Hasturkun :DHydrazine
No, it's not an HTML form, i want to upload the picture "nature.jpg" on a server, this server require a token 'AUTH_*******' to identify the user, so i've to send the file and the token (string) with the POST requestOperetta
@Hasturkun: i use 'curl' for few time, but it didn't work for me....i mean i don't know how to use it :)Operetta
@Maxime: ah i haven't seen the edit, thank's :) so in my case i will use : 'X-Auth-Token = *******' and 'Photo = nature.jpg' right ? Ps : X-Auth-Token is the name of the variableOperetta
@Dady: Sending file content as form data is unsupported by wget: Wget does not currently support "multipart/form-data" for transmitting POST dataDigenesis
That won't work. It's not that simple to upload files. You should try with curl for that.Hydrazine
@Maxime yes i mean with curl, such : curl -d "X-Auth-Token=AUTH_tk8c1eecae98e845159cebf69f06bb10f2" "Photo=nature.jpg "ipadress:8080/v1/AUTH_test/test" !! (but l've always authorization failed)Operetta
You should try to put the token in the header: curl -H "X-Auth-Token:AUTH_tk8c1eecae98e845159cebf69f06bb10f2" ...Hydrazine
Finaly it works : curl -v -H 'X-Auth-Token: AUTH_tk8c1eecae98e845159cebf69f06bb10f2' ipadress:8080/v1/AUTH_test/test -T nature.jpgOperetta

© 2022 - 2024 — McMap. All rights reserved.