Skip to content
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

Closed
pathikrit opened this issue Jan 17, 2019 · 5 comments
Closed

Add example to POST JSON #23

pathikrit opened this issue Jan 17, 2019 · 5 comments

Comments

@pathikrit
Copy link

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 API

@liancheng
Copy link
Contributor

liancheng commented Jan 22, 2019

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.

@pathikrit
Copy link
Author

I don't understand - how do I submit a json? Submit a Map and set content-type = application/json??

@liancheng
Copy link
Contributor

@pathikrit Submit a JSON string and set "Content-Type" to "application/json". I personally prefer using the ujson library to simplify the JSON string construction:

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()
)

@royki
Copy link

royki commented Jun 24, 2019

@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)

@lihaoyi
Copy link
Member

lihaoyi commented Jan 13, 2020

You can now skip the .render call:

requests.post(
  "https://api.github.com/some/endpoint",
  data = ujson.Obj("user-agent" -> "my-app/0.0.1")
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants