Home > Mobile >  Why does my PHP code return Error 400 "Your browser sent an invalid request."?
Why does my PHP code return Error 400 "Your browser sent an invalid request."?

Time:11-24

vv Request to API vv

    $data = $_POST['csrf'];
    $headers = [
        "x-csrf-token: $data\r\n".
        "Content-Type: application/json\r\n".
        "Accept: application/json\r\n"
    ];
    $data = <<<DATA
    {
        "username": "string",
        "password": "string",
        "gender": "Unknown",
        "birthday": "2021-11-22T23:29:51.656Z",
        "isTosAgreementBoxChecked": true,
        "email": "string",
        "locale": "string",
        "assetIds": [
            0
        ],
        "bodyColorId": 0,
        "bodyTypeScale": 0,
        "headScale": 0,
        "heightScale": 0,
        "widthScale": 0,
        "proportionScale": 0,
        "referralData": {
            "acquisitionTime": "2021-11-22T23:29:51.656Z",
            "acquisitionReferrer": "string",
            "medium": "string",
            "source": "string",
            "campaign": "string",
            "adGroup": "string",
            "keyword": "string",
            "matchType": "string",
            "sendInfo": true,
            "requestSessionId": "string",
            "offerId": "string"
        },
        "agreementIds": [
            "string"
        ],
        "identityVerificationResultToken": "string",
        "captchaId": "string",
        "captchaToken": "string",
        "captchaProvider": "string"
    }
    DATA;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://auth.roblox.com/v1/signup');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
}

http://api.aero-dev.xyz/bin/Captcha/CaptchaId.php?i=2 The server tells me "Error 400, Your browser sent an invalid request." The issue is with the x-csrf-token, when I put it as a string "x-csrf-token: j8acha7hffh" it works. When I put it as "x-csrf-token: $data" it returns an error. I have tried different ways of approaching this error. I have changed the headers. Tried different ways of requesting it. Still nothing fixes my error. I am new with PHP, please help!

CodePudding user response:

You are using the $header as an array however it contains long string as I suspect the line delimiter /r/n in the long string makes trouble here.

So change the one value array full of new line delimiters

$headers = [
    "x-csrf-token: $data\r\n".
    "Content-Type: application/json\r\n".
    "Accept: application/json\r\n"
];

to this array with multiple values

$headers = [
    "x-csrf-token: $data",
    "Content-Type: application/json",
    "Accept: application/json",
];

CURLOPT_HTTPHEADER An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

More details in https://www.php.net/manual/en/function.curl-setopt.php

CodePudding user response:

Try doing this:

"x-csrf-token: ".$data."\r\n".

  • Related