Search code examples
javascriptlaravelvue.jshttpcapacitor

How to catch Laravel errors using capacitor http plugin


I have some validations in Laravel. When using axios I used .catch() to get those errors. Due to some cors issues, I started to use @capacitor-community/http library as an alternative to axios in order to handle http requests on mobile. But I have no idea how to catch validation errors and other errors coming from Laravel through this plugin. I have attached my code below,

This is the validation part of my Laravel code

$request->validate([
   'username' => 'required | string',
   'email'    => 'required | email | unique:users,email',
   'password' => 'required | string | confirmed | min:8',
]

This is my vue code,

<script setup>
...
import { Http } from "@capacitor-community/http";

const registerUser = async () => {
  loadingState.value = true;
  const options = {
    url: `${requestURL.value}/register_app_user`,
    headers: { "Content-Type": "application/json" },
    data: userForm,
  };

  const response = await Http.request({ ...options, method: "POST" });
};

</script>

I'm receiving HTML content without receiving any errors. Really appreciate it if somebody could help thanks.


Solution

  • By including ,

    headers: { Accept: "application/json", "Content-Type": "application/json" },
    

    It worked