【问题标题】:405 error when fetching POST request. Why I cant do a POST request when Get request is OK?获取 POST 请求时出现 405 错误。为什么 Get 请求正常时我不能发出 POST 请求?
【发布时间】:2019-04-23 00:13:18
【问题描述】:

我正在尝试向 json blob api 发出 POST 请求(这是一个用于存储我的 JSON 文件的简单 api)。我收到 405 错误...
我不知道为什么我不能做 POST 请求,当 GOT 请求工作正常时..

有人可以帮帮我吗? https://jsonblob.com/api

   const api = "https://jsonblob.com/api/jsonBlob/c30c8afa-6557-11e9-acbe- 
   61e96b39ce8b"

    //it doesn't work
    fetch(api, {
        method: 'POST',
        body: JSON.stringify({
            name: 'dean',
            login: 'dean',
        })
    })
    .then(response => {
        if (response.ok) {
            return response.json()
        }
        throw new Error('Request failed!')
    })
    .then(jsonResponse => {
        console.log(jsonResponse)
    })
    .catch(error => {
        console.log('Request failure: ', error);
    });

   // get request works fine
   fetch(api).then((response) => {
        if (response.ok) {
            return response.json();
            console.log(response)
        }
        throw new Error('Request failed! ');
    })
    .then((Jsondata) => {
        console.log(Jsondata)
    })
    .catch(error => {
        console.log(error.message)
    });

【问题讨论】:

  • 哦,等等……您根本没有阅读 api 文档 - 为什么您的 POST 中有一个 blobid?

标签: javascript api post request fetch


【解决方案1】:

根据该 API 的文档,您需要在标头中指定 json 内容类型,这样可以正常工作:

fetch("https://jsonblob.com/api/jsonBlob", {
    method: 'POST',
    headers: {
      "Content-type": "application/json"
    },
    body: JSON.stringify({
        name: 'dean',
        login: 'dean',
    })
})
.then(response => {
    if (response.ok) {
        return response.json()
    }
    throw new Error('POST Request failed!')
})
.then(jsonResponse => {
    console.log(jsonResponse)
})
.catch(error => {
    console.log('POST Request failure: ', error);
});

【讨论】:

    【解决方案2】:

    如果您阅读了该 API 的文档,则 POST 请求不会在请求 URL 中使用 blobID - 您还需要添加带有值 application/jsoncontent-type 请求标头 - 否则您将收到 415 错误

    它在响应标头 x-jsonblob 中返回 Blob ID,因此,要获取 Blob ID 标头以供以后使用,您需要访问标头

    const api = "https://jsonblob.com/api/jsonBlob"
    
    fetch(api, {
        method: 'POST',
        body: JSON.stringify({
            name: 'dean',
            login: 'dean',
        }),
        // you also have to add this request header for that API
        headers: {
            'Content-Type': 'application/json'
        }
    }).then(response => {
        if (response.ok) {
            const blobID = response.headers.get('x-jsonblob');
            console.log(`POST returned a blobID = ${blobID}`);
            // return the blobID we can use to fetch the data later
            return blobID;
        }
        throw new Error('POST Request failed!')
    }).then(blobID => {
       // lets do a GET to see if we get the right data
       console.log(`fetch ${api}/${blobID}`);
       return fetch(`${api}/${blobID}`)
    }).then((response) => {
        if (response.ok) {
            return response.json();
        }
        throw new Error('GET Request failed! ');
    })
    .then((Jsondata) => {
        console.log('Result of GET')
        console.log(Jsondata)
    }).catch(error => {
        console.log('Request failure: ', error);
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-06
      • 2014-12-04
      • 2017-09-09
      • 2021-04-11
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多