1

I am trying to send a POST request to a site using Hyper 0.9. The request works with curl:

curl https://api.particle.io/v1/devices/secret/set_light -d args=0 -d access_token=secret

and Python:

import requests
r = requests.post("https://api.particle.io/v1/devices/secret/set_light",
    data={"access_token": "secret", "args": "0"})

but my Rust implementation doesn't seem to go through, always yielding 400.

use hyper::client::Client;

let addr = "https://api.particle.io/v1/devices/secret/set_light";
let body = "access_token=secret&args=0";
let mut res = client.post(addr)
                .body(body)
                .send()
                .unwrap();
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
luke
  • 1,024
  • 3
  • 11
  • 21
  • 1
    You should check what is really emitted in terms of HTTP; for example, the `=` and `&` in the body might be url-encoded accidentally. – Matthieu M. Jan 20 '16 at 08:19

1 Answers1

6

It is greatly beneficial to be aware of various tools for debugging HTTP problems like this. In this case, I used nc to start a dumb server so I could see the headers the HTTP client is sending (nc -l 5000). I modified the cURL and Rust examples to point to 127.0.0.1:5000 and this was the output:

cURL:

POST /v1/devices/secret/set_light HTTP/1.1
Host: 127.0.0.1:5000
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 26
Content-Type: application/x-www-form-urlencoded

args=0&access_token=secret

Hyper:

POST /v1/devices/secret/set_light HTTP/1.1
Host: 127.0.0.1:5000
Content-Length: 26

access_token=secret&args=0

I don't have an account at particle.io to test with, but I'm guessing you need that Content-Type header. Setting a User-Agent would be good etiquette and the Accept header is really more for your benefit, so you might as well set them too.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366