yeh
yeh

Reputation: 1506

How to use clj-http to post a request?

When I click the search button on this page, it sends a post request. I want to do the post via cli-http. How can I do that?

(def default-http-opts 
    {:socket-timeout 10000
     :conn-timeout 10000
     :insecure? true
     :throw-entire-message? false})

(clj-http/post initial-url default-http-opts)

can post a request but the problem is that I want to pass in some parameters. These parameters(the buttons selected) are default on the page.

They are:

AdvancedSearchForm:CourseOrSubjectSelection=ALL_ALL
AdvancedSearchForm:GraduateLevelSelection=ALL

AdvancedSearchForm:allStudyAreas=t

AdvancedSearchForm:departmentList=

AdvancedSearchForm:facultyList=

AdvancedSearchForm:keywords=

AdvancedSearchForm:level=ALL

AdvancedSearchForm:semester=ALL

oracle.adf.faces.FORM=AdvancedSearchForm

oracle.adf.faces.STATE_TOKEN=_id21519:_id21520

source=AdvancedSearchForm:searchButton

The key AdvancedSearchForm:semester contains ':', so I use string as a key like this "AdvancedSearchForm:semester", is it OK in clj-http?

I do it like this:

(spit (file "/tmp" "ts.html") 
    (:body (http/post initial-url 
        {:form-params {"AdvancedSearchForm:CourseOrSubjectSelection" "ALL_ALL",                          "AdvancedSearchForm:GraduateLevelSelection" "ALL"}})))`

Actually the page it returns is indeed "Results" but no courses are listed. only the template. I want to get all the course links which are only shown by manually click. Any help?

This is the image I screenshot from Tamper Data. It shows what happens after I click the Search button. Seems like client is redirected to searchresult.jsp. I use curl to imitate that. I do it like this

curl -D "form data..." https://handbook.unimelb.edu.au/faces/htdocs/user/search/AdvancedSearch.jsp

Then quickly run

curl https://handbook.unimelb.edu.au/faces/htdocs/user/search/SearchResults.jsp

No results contents are shown though the page is downloaded.

Upvotes: 0

Views: 7668

Answers (2)

claj
claj

Reputation: 5402

It looks like the server doesn't understand the parameters you send to it.

The escaping in use is the percent-encoding. Try to check out if it get in use by using the debug functionality avail in clj-https README.md:

;; print request info to *out*, including request body:
(client/post "http://example.org" {:debug true :debug-body true :body "..."})

or try to manually run the requests either with the curl command in a terminal or with the convenient Firefox restclient add-on.

Upvotes: 2

ponzao
ponzao

Reputation: 20934

From their GitHub page (https://github.com/dakrone/clj-http):

;; Send form params as a urlencoded body (POST or PUT)
(client/post "http//site.com" {:form-params {:foo "bar"}})

Upvotes: 1

Related Questions