question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

post with form-params and content-type json

See original GitHub issue

I am programming against an internal REST API that requires a POST, with form-params, and content-type json.

I’ve verified that I can connect to the API, login, get cookies, and do other simple things. I have written working code against this API in Python and have access to similar Ruby code.

Here are two tries. One using clj-http (which works) and the other http-kit, which does not.

(defn post-weblink-http-kit [path cookies spot-id url title description]
  @(http-kit/post (str path "spots/" spot-id "/items")
              {:headers {"Cookie" cookies}
               :content-type :json  ; Ignored??
               :form-params {:item {:webLink {:url url :title title :description description}}}}))


(defn post-weblink-clj-http [path cookies spot-id url title description]
  (client/post (str path "spots/" spot-id "/items")
               {:cookies cookies
                :content-type :json
                :form-params {:item {:webLink {:url url :title title :description description}}}}))

(Note, the only real difference here is that the cookies for http-kit are raw and the cookies for clj-http are a map. But I have verified that is not the issue.)

I have also tried explicitly constructing the json (which is what I needed to do in Python).

(defn post-weblink-json [path cookies spot-id url title description]
  @(http-kit/post (str path "spots/" spot-id "/items")
              {:headers {"Cookie" cookies}
               :content-type :json  ; Ignored??
               :form-params {:item (json/generate-string {:webLink {:url url :title title :description description}})}}))

Both http-kit calls give an error like this:

:body “"757: unexpected token at ‘item=%7B%22webLink%22%3A%7B%22url%22%3A%22http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNikola_Tesla%22%2C%22title%22%3A%22Nik+Tesla%22%2C%22description%22%3A%22foo+bar+baz%22%7D%7D’"”

Any thoughts on this??

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
pastafaricommented, Aug 22, 2014

Unfortunately, if you pass in form-params, http-kit tries to coerce it as below:

:body      (if form-params (query-string form-params) body))
;;; client.clj Line 72

so even though you constructed json, it tried to coerce it into a url encoded string. The :content-type was ignored as you rightly point out.

The trick is to pass the :body and :headers explicitly as below:

(http/post some-url {:body (json/write-str {"item" "blahblah"})
                                :headers {"Content-Type" "application/json"}})

That said, I agree that the interface of http-kit could be a little friendlier.

0reactions
ptaoussaniscommented, Dec 16, 2015

Closing this, thanks for the docs PR!

Read more comments on GitHub >

github_iconTop Results From Across the Web

sending file and json in POST multipart/form-data request with ...
To set a content-type you need to pass a file-like object. You can create one using a Blob . const obj = {...
Read more >
How to use fetch to POST form data as JSON to your API
Step 1. Listen for when a user submits the form · Step 2. Read the values of all the form fields with FormData...
Read more >
How To Format Form Data as JSON - Section.io
This article will walk the reader through how to capture form field data, format them as JSON data and send it to an...
Read more >
Having trouble sending an HTTP POST request with form-data ...
Though I'm sending form-data, if I don't specify the Content-Type in the request header, it gets detected as "application/json" and I get ...
Read more >
Part 5 - Postman - Http Post (JSON Body And File Upload ...
Request Payload – Normal Object/ JSON Document ( Content-Type Header ... Http Post ( JSON Body And File Upload) (Raw and Multipart Formdata...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found