Using curl to test POST data: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 19: Line 19:
$ curl -u mylogin:myp\&ss --ntlm -d "user=mylogin&pass=mypass&foo=bar&biz=bash" http://www.mydomain.com/mypage/
$ curl -u mylogin:myp\&ss --ntlm -d "user=mylogin&pass=mypass&foo=bar&biz=bash" http://www.mydomain.com/mypage/
</syntaxhighlight>
</syntaxhighlight>
==Storing arguments in a text file==
 
== Storing arguments in a text file ==
 
Content of file, saved as <code>curlargs.txt</code>:
Content of file, saved as <code>curlargs.txt</code>:
<pre>
<pre>
Line 28: Line 30:
$ cat curlargs.txt | xargs -n3 curl
$ cat curlargs.txt | xargs -n3 curl
</syntaxhighlight>
</syntaxhighlight>
== Handling quotes in POST data ==
'''Problem:''' The value of the `-d` or `--data` argument (typically a JSON string) contains either a single or double quote:
<syntaxhighlight lang="bash">
# Error is thrown when it hits the first quote in the ''title'' string.
curl -d '{ "id": "6650", "title": "A record title containing 'quotes'."}' http://mydomain.com/path/to/page/
</syntaxhighlight>
'''Solution:''' Use the `@` character to read the data from a separate file.
Curl arguments stored in `curargs.txt`:
<pre>
-d @jsondata.txt http://mydomain.com/path/to/page/
</pre>
Contents of `jsondata.txt`:
<pre>
{ "id": "6650", "title": "A record title containing 'quotes'."}
</pre>
Then pass the contents of the two files to curl with
<syntaxhighlight lang="bash">
$ cat curlargs.txt | xargs -n3 curl
</syntaxhighlight>
[[Category:Web Development]]
[[Category:Web Development]]

Revision as of 17:06, 21 February 2013

Basic request passing variables as POST

Use --data or -d option to pass variables to the page.

$ curl -d "user=mylogin&pass=mypass&foo=bar&biz=bash" http://www.mydomain.com/mypage/

Request using basic authentication

Use --user or -u option.

$ curl -u mylogin:mypass -d "user=mylogin&pass=mypass&foo=bar&biz=bash" http://www.mydomain.com/mypage/

Request using Windows integrated authentication

Add --ntlm option.

$ curl -u mylogin:mypass --ntlm -d "user=mylogin&pass=mypass&foo=bar&biz=bash" http://www.mydomain.com/mypage/

Special characters in username or password

Escape special characters with back slash.

$ curl -u mylogin:myp\&ss --ntlm -d "user=mylogin&pass=mypass&foo=bar&biz=bash" http://www.mydomain.com/mypage/

Storing arguments in a text file

Content of file, saved as curlargs.txt:

-d foo=bar&biz=bash http://localhost/mytestpage.html

Run curl using contents of curlargs.txt (in a bash shell):

$ cat curlargs.txt | xargs -n3 curl

Handling quotes in POST data

Problem: The value of the -d or --data argument (typically a JSON string) contains either a single or double quote:

# Error is thrown when it hits the first quote in the ''title'' string.
curl -d '{ "id": "6650", "title": "A record title containing 'quotes'."}' http://mydomain.com/path/to/page/


Solution: Use the @ character to read the data from a separate file.

Curl arguments stored in curargs.txt:

-d @jsondata.txt http://mydomain.com/path/to/page/

Contents of jsondata.txt:

{ "id": "6650", "title": "A record title containing 'quotes'."}

Then pass the contents of the two files to curl with

$ cat curlargs.txt | xargs -n3 curl