Skip to content

Document example of sending a Dict to POST API #281

@s-celles

Description

@s-celles

Hello,

I'm facing a difficulty (maybe because doc need some improvements) to convert a Julia 0.6 code which was using Request.jl to a Julia 0.7/1.0 code using HTTP.jl

This is my original code

using Requests: post, json
raw_response = post(url_query; data = params)
response = json(raw_response)

I tried this

using HTTP
using JSON
raw_response = HTTP.request("POST", url_query, params)
response = JSON.parse(raw_response)

but API endpoint ( see https://pushover.net/ and https://pushover.net/api for my use case) returned

ERROR: HTTP.ExceptionRequest.StatusError(400, HTTP.Messages.Response:
"""
HTTP/1.1 400 Bad Request
Server: nginx
Date: Sun, 19 Aug 2018 09:30:08 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, X-Prototype-Version, Origin, Accept, Content-Type, X-CSRF-Token, X-Pushover-App, Authorization
Access-Control-Max-Age: 1728000
Cache-Control: no-cache
X-Request-Id: fca220b7-291f-4526-845f-99afd114f0f7
X-Runtime: 0.007232

{"message":"cannot be blank","errors":["message cannot be blank"],"status":0,"request":"fca220b7-291f-4526-845f-99afd114f0f7"}""")

My parameters dict looks like

julia> params
Dict{String,String} with 4 entries:
  "priority" => "1"
  "token"    => "NzXXXXXXXXXXXXXnp"
  "message"  => "My first message"
  "user"     => "RAXXXXXXXXXXXXjN"

I also tried

HTTP.request("POST", url_query, [], JSON.json(params))

but it still raise same exception.

Any idea what is going wrong here and how/if doc need to be improved or not (if that's the case I will submit a PR)

Kind regards

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions