Reputation: 55
I am working on a Twitter API for Rust and am running into issues with passing arguments to my GET request. The code I'm using for the request is given below. data_body
is something like "screen_name=a_user" and the authorization header is the OAuth authorization required by Twitter (this isn't the issue as it's working with all my other requests).
let mut res = client.get("http://httpbin.org/get")
.body(&data_body[..])
.header(Authorization(authorization_header))
.header(ContentType::form_url_encoded())
.send().unwrap();
I started sending this to httpbin.org so I could see the raw request. What I'm getting back is shown below.
{
"args": {},
"headers": {
"Authorization": "OAuth oauth_consumer_key=\"\", oauth_nonce=\"RH9vZYMCbAtfQAuVh44fhlyGQfQqBCPq\", oauth_signature=\"dTbOyaURct0auVlC%2B8Y7vDFpus0%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1452648640\", oauth_token=\"\", oauth_version=\"1.0\"",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org"
},
"origin": "0.0.0.0",
"url": "http://httpbin.org/get"
}
The curl request given by the Twitter api docs have a data portion for the GET request below (I've replaced my keys with empty strings). I can confirm this works for the GET.
curl --get 'https://api.twitter.com/1.1/users/show.json' --data 'screen_name=twitterdev' --header 'Authorization: OAuth oauth_consumer_key="", oauth_nonce="6a37886cb38b2f5e12dee8fd47aa097c", oauth_signature="zhXDA5JbKRmw2xbJcEqK9sxuu5E%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1452647580", oauth_token="", oauth_version="1.0"' --verbose
I've tried a bunch of different ContentType
s from the Hyper API but can't get the args section of the HTTP request populated. I think that's the issue, but I don't have a lot of experience with HTTP so I could very well be wrong. Ultimately the request is returning a 403 (Unauthorized) which is triggered by the missing args. I know my OAuth header is being generated correctly because it's working on all the POST requests and when I copy over the nonce and timestamp from the curl command I get from the Twitter API the signature matches.
Upvotes: 1
Views: 3963
Reputation: 10180
As @hamersaw said, "GET parameters" are part of URL, in the query string. If you need to set parameters programatically (if they’re not always the same), the Url::set_query_from_pairs
method can take care of the escaping/encoding for you:
use hyper::Url;
let mut url = Url::parse("https://api.twitter.com/1.1/users/show.json").unwrap();
url.set_query_from_pairs([
("screen_name", "twitterdev"),
].iter().cloned());
let res = client.get(url) // ...
Upvotes: 4
Reputation: 55
Figured it out. Turns out in a GET request you have to append the body to the request and it will automatically fill out in the packet. Below is the code to get it working.
let mut res = client.get("http://httpbin.org/get?screen_name=twitter")
.header(Authorization(authorization_header))
.send().unwrap();
Upvotes: -1