0

I am trying to reverse engineer some php code and then run it in JS. The php code is as follows:

$fields_string = '';
    //url-ify the data for the POST
    foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

    //execute post
    $result = curl_exec($ch);

    //decode result;
    $data = json_decode($result, TRUE);
    //close connection
    curl_close($ch);
    return $data;

I am using axios in JS and currently trying the following:

const response: any = await axios.post(base_url, 'key1=value1&key2=value2', {
        headers: {
            'Content-Type': "multipart/form-data"
        }
    });

But I seem to be getting issues as the server isn't able to parse the body correctly. I was wondering if anyone had any tips for what it should look like instead?

1 Answer 1

1

In order to send form-data with axios you should pass FormData obj as data, it will change to the proper header.

const formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');

const response = await axios.post(base_url, formData);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.