luke
luke

Reputation: 1074

Hyper POST request always yields 400

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

Upvotes: 1

Views: 1760

Answers (1)

Shepmaster
Shepmaster

Reputation: 430300

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.

Upvotes: 6

Related Questions