首页 > 解决方案 > AWS POST 到 API Gateway 返回 415“不支持的媒体类型”

问题描述

我目前正在尝试直接设置对我的 DynamoDB 表的 API Gateway 代理调用。我已设置好所有内容,以便 API Gateway 控制台中的测试返回预期结果:

{
  "Items": [
    {
      "zoneID": "Northern",
      "location": "Pro Skate Shop",
      "address": "123 Fake Street",
      "exposureDate": "04/06/2021",
      "exposureWindow": "16:00 - 17:30",
      "advice": "get tested immediately"
    }
   ]
 }

如果我curl是我的端点,我也会得到预期的响应:

$ curl -X POST https://<API URL>/dev/getcontacts
{
    "Items": [
         {
            "zoneID": "Northern",
            "location": "Pro Skate Shop",
            "address": "123 Fake Street",
            "exposureDate": "04/06/2021",
            "exposureWindow": "16:00 - 17:30",
            "advice": "get tested immediately"
          }
     ]
  }

但是,当我从我的应用程序调用时:

export function GetContacts() {
    const [contacts, setContacts] = useState([]);

    const url = '/getcontacts'

    useEffect(() => {
        async function callApi() {
            try {
                const contactList = await API.post('testapi', url)
                console.log("results: ", contactList)
                setContacts(contactList.Items)
            } catch (err) { console.log({err})}
        }
        if (contacts.length === 0) {
            callApi()
        }
    }, [contacts, url])

我在控制台中收到以下错误:

Failed to load resource: the server responded with a status of 415 ()

我假设这只是与请求标头有关(它被发送为application/json),但我真的不确定如何解决这个问题。

任何帮助将不胜感激!

标签: javascriptamazon-web-servicesaws-amplify

解决方案


推荐阅读