http-rs / http-rs/surf

Post form not working

Open
#132 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
1.5k
Forks
128
PR merge metrics
No merged PRs in 30d

Description

```rust
use surf::http::StatusCode;
use surf::Response;
use serde::{Deserialize, Serialize};

pub(crate) struct FreshAccount<'a> {
pub username: &'a str,
pub password: &'a str,
}

#[derive(Serialize, Deserialize)]
struct LoginForm<'a> {
ig_sig_key_version: &'a str,
signed_body: &'a str
}

pub(crate) async fn login(fresh: FreshAccount<'_>) -> Option<(&str, &str)> {
println!("Attempting to login with credentials ({}:{})", fresh.username, fresh.password);

let mut csrf_token: &str = "";
let mut session_id: &str = "";

let uri: &str = "https://i.instagram.com/api/v1/accounts/login/";

let fresh_uuid = uuid::Uuid::new_v4().to_hyphenated();

let body: String = format!("fa61f4be32e827c7152e38a075e3614\
2d8313ba582d6437f07539b00a03f454e.{{\"reg_login\":\"0\",\"password\":\"{}\",\"device_id\":\
\"{}\",\"username\":\"{}\",\"adid\":\"FE4FD084-9DCB-481A-A248-57E0E32E25ED\",\
\"login_attempt_count\":\"0\",\"phone_id\":\"{}\"}}", fresh.password, fresh_uuid, fresh.username, fresh_uuid);

println!();
println!("Signed body is {}", body);

let mut res: Response = surf::post(uri)
.set_header("Accept-Language", "en-us en q=0.9")
.set_header("Accept-Encoding", "gzip, deflate, br")
.set_header("Content-Type", "application/x-www-form-urlencoded")
.set_header("Connection", "keep-alive")
.set_header("User-Agent", "Instagram 44.0.0.17.95 (iPhone9,3; iOS 12_0; en_US; en-US; scale=2.00; gamut=wide; 750x1334) AppleWebKit/420+")
.body_form(&LoginForm{ig_sig_key_version: "5", signed_body: &body}).unwrap().await.expect("Unwrap for response of login request failed..");

let status: StatusCode = res.status();

println!();
if status == 200 {
// get from headers
csrf_token = "csrftokenhere";
session_id = "sessionidhere";
} else {
println!("Unexpected status code {}", status.as_str());
println!("{}", res.body_string().await.unwrap());
}

// Failed == session_id is empty
Some((csrf_token, session_id))
}
```
Golang code: https://pastebin.com/9C1HUkTV

I'm getting a 400 response code (bad request), golang is giving me a 200 response code for the same request

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.