Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

REST curl helper

// A small bash script that wraps around curl to help more easily test RESTful sites.

#!/bin/bash
AUTH="user:password"
BASE="http://localhost:3000"
METHOD=$1
DEST="$BASE$2"
XML=$3

# make sure args were passed
if [ $# -eq 0 ]; then
        echo "usage: ./`basename $0` HTTP-METHOD DESTINATION_URI [XML]"
        echo "example: ./`basename $0` POST "/accounts" \"<account><name>ed</name><email>ed@ed.com</email></account>\""
        exit 1
fi

# execute CURL call
curl -H 'Accept: application/xml' -H 'Content-Type: application/xml' -w '\nHTTP STATUS: %{http_code}\nTIME: %{time_total}\n' \
-X $METHOD \
-d "$XML" \
-u "$AUTH" \
"$DEST"

exit 0

Amazon REST API for newLISP

;; (amazon-op "Operation=ItemSearch&SearchIndex=Books&ItemPage=1&Keywords=Cloud+Atlas&Respon\
seGroup=Request,Small")
;;
;; (amazon-op "Operation=SimilarityLookup&ItemId=1400063795,0812966929&SimilarityType=Random\
&ResponseGroup=Request,Large")
;; (amazon-op "Operation=SimilarityLookup&ItemId=1400063795,061873516X,0812966929&Similarity\
Type=Random&ResponseGroup=Request,Small")

;;  (setq x (xml-digest (amazon-op "Operation=ItemLookup&ItemId=0375507256&ResponseGroup=Req\
uest,Similarities,ListmaniaLists")))
;; Using x get a list of listmanias for an item
;;  (map (lambda (x) (x 1 2 2)) (1 -1 (x (chop (ref 'ListmaniaLists  x)))))

;; (setq y (amazon-op "Operation=ListLookup&ListType=Listmania&ListId=R21LV6VJEZ794O&Respons\
eGroup=ListFull"))
;; Using y get a list of ISBNs from a listmania
;; (map (lambda(x) (x 4 1 2 )) (8 -1 ( (y (chop (ref 'Lists y)) ) 2))) ; gives list of ISBNs\
 from the listmania

;; get a list of listmania details from a list of listmanias.
;;  (setq z   (map (lambda (x) (xml-digest (amazon-op (append "Operation=ListLookup&ListType\
=Listmania&ListId=" x "&ResponseGroup=ListFull"))))  (map (lambda (x) (x 1 2 2)) (1 -1 (x (c\
hop (ref 'ListmaniaLists  x))))))  )
;;
;; get a list of ISBNs from all of the listmanias for the item
;; (setq z (flat (map (lambda(y) (map (lambda(x) (x 4 1 2 )) (8 -1 ( (y (chop (ref 'Lists y)\
) ) 2))) ) z)))
;;

;; get item info for all the ISBNs from all listmanias related to the original item
;; (setq w (map (lambda (x) (xml-digest (amazon-op (append "Operation=ItemLookup&ItemId=" x)\
)))  (unique z)))

;; get a list of pair (title,author) from w above.
;; (map (lambda (x) (list ((x (chop (ref 'Title x))) 1)  (if (ref 'Author x)((x (chop (ref '\
Author x)))1) "???"))) w)

(define (amazon-op params)
  (get-url (append 
"http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&&AWSAccessKeyId=YOUROWNKEY&" params)))

(define (xml-digest result)
  (xml-type-tags nil nil nil nil)
  (setq xresult (xml-parse result (+ 1 2 4 8 16))))

Post XML using curl

You can use curl on the command line to do a POST to an endpoint.

echo '<doc><item>Some content.</item></doc>' | curl -X POST -H 'Content-type: text/xml' -d @- http://example.com/restapi


This is handy for adding web services to applications that do not do web services but can do command lines--FileMaker for example.
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS