-
-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example to POST JSON #23
Comments
The wrong header issue should have been fixed by #26 (assuming that you meant the "Content-Type" header set to "text/plain"), which is available in 0.1.7. |
I don't understand - how do I submit a json? Submit a Map and set |
@pathikrit Submit a JSON string and set "Content-Type" to "application/json". I personally prefer using the import $ivy.`com.lihaoyi::requests:0.1.7`
import $ivy.`com.lihaoyi::ujson:0.7.1`
import ujson._
requests.post(
url = "https://httpbin.org/post",
headers = Map("Content-Type" -> "application/json"),
data = Obj(
"hello" -> "world",
"this" -> Arr("is", "an", "array")
).render()
) |
@pathikrit You can do it following way as well - import requests._
import ujson._
// headers
val allHeaders = Map(
"x-api-key" -> x-api-key
"Authorization" -> authorization,
"Accept" -> "application/json",
"Content-Type" -> "application/json"
)
// some Json Object
import ujson._
val userJsonReq = Js.Obj(
"login" -> s"$randomStr@yopmail.com",
"password" -> s"$randomStr",
"phoneNumber" -> s"$phone",
"email" -> s"$randomStr@yopmail.com",
"firstName" -> "Name",
"lastName" -> "LastName",
"gender" -> "female",
"address" -> Js.Obj(
"streetAddress" -> s"$address",
"locality" -> "Lo",
"postalCode" -> "XXXX",
"region" -> "NA",
"country" -> "US"
)
)
// Post Request with headers and request body
val createUser = requests.post(
"https://www.yourapi.com",
headers = allHeaders,
data = userJsonReq.toString
)
val json = ujson.read(createUser.text())
println(json) |
You can now skip the requests.post(
"https://api.github.com/some/endpoint",
data = ujson.Obj("user-agent" -> "my-app/0.0.1")
) |
When we submit
data = Map
we get a form submission; if it is a JSON encoded as String, we get the wrong header. It would be helpful if the README included an example of submitting a JSON to a JSON APIThe text was updated successfully, but these errors were encountered: