0

Can you please help me to convert this curl post request into a javascript(Axios) request?

    
https://url.com/api/send.php

function send_sms($url,$apikey,$smsno=1,$destination,$content){
    $fields = array(
       'apikey' => $apikey,
       'smsno' => $smsno,
       'destination' => $destination,
       'content' => $content
    );
    $fields_string = http_build_query($fields);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $output;
}
1
  • If you have a Bash curl command (not PHP code), you can use curlconverter.com/node-axios Commented Sep 20, 2022 at 5:30

2 Answers 2

3

Using fetch can help you convert a CURL command to fetch code.

Since fetch is open source, you can modify or extend the source code at will.


You can also copy a request recorded with the Chrome DevTools as a fetch.

https://github.com/kigiri

Source: kigiri

Sign up to request clarification or add additional context in comments.

Comments

0

The following example will do the job:

function send_sms(apikey, smsno, destination, content){
    axios({
        method: 'post',
        url: 'https://url.com/api/send.php',
        data: {
            apikey: apikey,
            smsno: smsno,
            destination: destination,
            content: content
        }
    }).then((response) => {
        console.log(response);
    }, (error) => {
        console.log(error);
    });
}

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.