1

I am performing the following POST request from my React component:

axios({
    method: 'post',
    url: '/build',
    data: JSON.stringify({
        title: stateCopy.title,
        description: stateCopy.value,
        hunter: stateCopy.selectedHunter.id,
        encryption: stateCopy.encryption
    })
})
.then(response => {
    console.log(response)
})
.catch(errors => {
    console.log("error: " + errors)
});

which posts to my Laravel controller where I would like to access title, description, hunter and encryption.

I tried a few options such as request('title'), request()->post() and $request->getContent() but was unable to access what I want. Could someone point me in the right direction?

2 Answers 2

3

You have to decode stringified object

$data = json_decode($request->getContent());

$title       = $data->title;
$description = $data->description;
$hunter      = $data->hunter;
$encryption  = $data->encryption;

Hope this helps.

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

Comments

0

Use Laravel request method "getContent" to get the data passed.

$content = json_decode($request->getContent());

If you like to send parsed JSON as object. Use,

data: JSON.parse(
    JSON.stringify({
        title: stateCopy.title,
        description: stateCopy.value,
        hunter: stateCopy.selectedHunter.id,
        encryption: stateCopy.encryption
    })
)

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.