You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ionic/Cordova开发APP时POST请求遇415错误求助

Fixing 415 Unsupported Media Type Error in Ionic/Cordova POST Requests

Hey there, let’s work through that 415 error you’re facing when trying to upload scan data to your REST API. I’ve run into similar issues with Ionic apps before, so let’s break down the most likely causes and fixes:

1. Double-Check Your Request Headers (And How You’re Sending Them)

A 415 error usually means the server doesn’t recognize the media type of your request body. First, make sure you’re explicitly setting both Content-Type and Accept headers to application/json—some servers are strict about both.

If you’re using Angular’s HttpClient:

import { HttpClient, HttpHeaders } from '@angular/common/http';

// ...

const httpHeaders = new HttpHeaders({
  'Content-Type': 'application/json',
  'Accept': 'application/json'
});

const scanData = {
  scanId: '12345',
  content: 'scanned-value-here',
  timestamp: new Date().toISOString()
};

// Use the object directly—HttpClient automatically stringifies it!
this.http.post('https://your-api-endpoint.com/upload', scanData, { headers: httpHeaders })
  .subscribe(
    res => console.log('Upload successful:', res),
    err => console.error('Upload failed:', err)
  );

Important: Don’t manually call JSON.stringify() here—Angular’s HttpClient does that for you. If you stringify the data yourself, you’ll end up sending a string instead of a JSON object, which can trigger a 415.

If you’re using a Cordova HTTP plugin (like cordova-plugin-advanced-http):

These plugins often handle header and data formatting differently. Some automatically convert objects to JSON, so avoid stringifying your data:

import HTTP from '@ionic-native/http/ngx';

// ...

const headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json'
};

const scanData = {
  scanId: '12345',
  content: 'scanned-value-here',
  timestamp: new Date().toISOString()
};

this.http.post('https://your-api-endpoint.com/upload', scanData, headers)
  .then(res => console.log('Success:', res.data))
  .catch(err => console.error('Error:', err));

If you’re still having issues, check the plugin docs—some require you to enable JSON encoding explicitly.

2. Verify Your JSON Data is Valid

Even if you’re stringifying correctly, invalid JSON can cause the server to reject your request. Use a tool like JSONLint to paste the output of JSON.stringify(scanData) and confirm it’s valid. Look for missing commas, unescaped quotes, or malformed date strings—these are common culprits.

3. Inspect the Actual Request Being Sent

Use your browser’s DevTools (if testing in a browser) or a proxy tool like Charles Proxy to capture the full request. Check:

  • Did the Content-Type: application/json header actually get sent? Sometimes framework defaults or plugin quirks can override your headers.
  • Is the request body exactly the JSON structure the server expects? For example, if the API requires a nested data object (like { "data": { ... } }), sending the flat object will fail.

4. Rule Out Server-Side Configuration Issues

Since you don’t control the server, it’s worth reaching out to the team that manages it to confirm:

  • Are they definitely accepting application/json for this endpoint?
  • Do they require any additional headers (like authentication tokens) that you’re missing?
  • Is there a specific JSON schema your data must follow (e.g., required fields, data types)?

5. Try a Different HTTP Client

If you’re using Angular’s HttpClient and hitting walls, switch to a Cordova-native HTTP plugin. These plugins bypass browser CORS restrictions and often handle low-level request details more reliably for mobile apps.


内容的提问来源于stack exchange,提问作者sunnyj58

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:12:50