0

I want to upload a file using webform_rest API as described here. #2899902. But it gives me 415 Unsupported Media Type error as you can see in the screenshot.

Console error screenshot

Here is my code

  methods: {
    addImage() {
      this.dropFiles = event.target.files[0];
    },

    submitResume() {
      const formData = new FormData();
      formData.append("image", this.dropFiles, this.dropFiles.name);

      axios
        .get("https://example.com/rest/session/token")
        .then(tokenResponce => {
          this.formToken = tokenResponce.data;
        });

      axios
        .post(
          "https://example.com/webform_rest/job_applications/upload/resume?_format=json",
          formData,
          {
            headers: {
              Accept: "json",
              "Content-Type": "application/hal+json",
              "Content-Disposition": "file; filename = full.pdf",
              "X-CSRF-Token": this.formToken
            }
          }
        )
        .then(response => {
          this.submitForm(response);
        });
    },

    async submitForm(response) {
      if (this.page === "singlejob") {
        var position = this.jobs.title;
      } else {
        var position = this.position;
      }

      let config = {
        headers: {
          Accept: "json",
          "Content-Type": "application/hal+json"
        }
      };

      this.isLoading = true;

      axios
        .post(
          `https://example.com/webform_rest/submit?_format=json`,
          {
            webform_id: "job_applications",
            name: this.name,
            email: this.email,
            position: position,
            resume: response.data.fid[0].value
          },
          config
        )
        .then(response => {
          this.isLoading = false;
          if (response.data.sid) {
            this.$router.push({
              path: "/thank-you"
            });
          }
        })
        .catch(error => (this.isLoading = false));
    }
  }

Need help here. Thanks

0

I got it working. two things I was missing.

  1. Make sure you accept all the operations / JSON requests from here /admin/config/services/jsonapi
  2. I was confused with "binary file data". So basically you don't have to do anything with the file. No formData, No base64, just pass your file variable as it is.

And it worked.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.