Wednesday, December 9, 2015

cURL Example: Post a JSON File with Basic Auth

The web was missing a clear example that showed how to POST a JSON file with Basic Auth.  I love using cURL for it's simplicity when trying out api's and other services that I might want to use and have spent a decent amount of time figuring this particular usage out more than once.  If nothing else, I'll be helping myself next time.

cURL JSON + Basic Auth Samples


In this case, I have a file locally that's contents are the JSON which I want to be the payload of the POST.  Note that I do not want to POST the file as with a multi-part form upload.

curl -X POST -d @pathtofile https://user:pass@www.samplesite.com



Depending on the service you are calling, you might also need to set the Content-Type and encoding.

curl --header "Content-Type: application/json;charset=UTF-8" -X POST -d @pathtofile https://user:pass@www.samplesite.com


Finally, for troubleshooting, I've found it useful to either use the verbose -v for inspecting headers or --trace-ascii /dev/stdout for seeing the content of the request

curl -X POST -d @pathtofile https://user:pass@www.samplesite.com -v

curl -X POST -d @pathtofile https://user:pass@www.samplesite.com --trace-ascii /dev/stdout


Sample Output of --trace-ascii /dev/stdout command


== Info: Trying aa.xx.dd.bb...
== Info: Connected to xyz.abcd.com (aa.xx.dd.bb) port 443 (#0)
== Info: TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
== Info: Server certificate: xyz.abcd.com
== Info: Server auth using Basic with user 'admin'
=> Send header, 241 bytes (0xf1)
0000: POST /demo HTTP/1.1
0031: Host: xyz.abcd.com
004f: Authorization: Basic Z3JlZW5oYXRhZG1pbjoxMjNBZG1pbiE=
0086: User-Agent: curl/7.43.0
009f: Accept: */*
00ac: Content-Type: application/json;charset=UTF-8
00da: Content-Length: 596
00ef:
=> Send data, 596 bytes (0x254)
0000: { "Students": [{ "MIS_ID": 201073, "Forename": "Test", "Surname
0040: ": "Tester", "Email": "xyz.abcd@gmail.com", "YearGroup": "6
0080: ", "Gender": "M", "Password": "Ab@12" }, { "MIS_ID": 161201, "Fo
00c0: rename": "Tester", "Surname": "Test", "Email": "xyz.abcd@gm
0100: ail.com", "YearGroup": "6", "Gender": "F", "Password": "Ab@12
0140: " }], "Staff": [{ "TeacherID": 220380, "Title": "Mrs", "Forename
0180: ": "Test", "Surname": "Tester", "Email": "xyz.abcd@gmail.
01c0: com" }], "Groups": [{ "GroupID": 63, "GroupName": "6A Science", "
0200: GroupType": "Class", "GroupDescription": "6A Science", "PrimaryS
0240: taffId": 220380 }] }
== Info: upload completely sent off: 596 out of 596 bytes
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 52 bytes (0x34)
0000: Cache-Control: no-cache, no-store, must-revalidate
<= Recv header, 47 bytes (0x2f)
0000: Content-Type: application/json; charset=utf-8
<= Recv header, 37 bytes (0x25)
0000: Date: Wed, 09 Dec 2015 16:02:52 GMT
<= Recv header, 40 bytes (0x28)
0000: Expires: Thu, 19 Jun 1980 19:19:19 GMT
<= Recv header, 18 bytes (0x12)
0000: Pragma: no-cache
<= Recv header, 15 bytes (0xf)
0000: Server: nginx
<= Recv header, 45 bytes (0x2d)
0000: Strict-Transport-Security: max-age=31536000
<= Recv header, 23 bytes (0x17)
0000: Vary: Accept-Encoding
<= Recv header, 20 bytes (0x14)
0000: Content-Length: 25
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 25 bytes (0x19)
0000: {"i":"4T7hz8tz8KU7ms2rz"}
== Info: Connection #0 to host xyz.abcd.com left intact
{"i":"4T7hz8tz8KU7ms2rz"}%

No comments:

Post a Comment