3

I'm doing a get method, when i run the code i'm getting this Did not attempt to load JSON data because the request Content-Type was not 'application/json'.. I have tried to set headers. Here is my code.

<template>
   <div class="container">
     <button @click="SearchTown"> Search Town </button>

   </div>
</template>

<script>
import axios from 'axios';
export default {
        name: 'SearchTown',
        props: {
            messege: String
        },
  data(){
    return{
      search: [],
    }
    
    },

  methods :{
      SearchTown() {
    axios
      .get('https://david.darwinist.io/proxy/5000/town/',{headers:{'content-type':'application/json'}})
     .then((response) => {// checking response in the console
            console.log(response.data)})
      .catch((error)=>{
        console.log(error)
      })
  }
 }
}

</script>

I'm having 400 error code. I need help.

This is my backend code

def getTown(session, town_dict):
    try:
        town = (
            session.query(Town)
            .join(Town.county)
            .join(County.nation)
            .where(County.name == town_dict["county"])
            .where(Town.name == town_dict["name"])
            ).one()
        town_county = copy.copy(town).__dict__
        del town_county["_sa_instance_state"]
        town_county["county"] = town.county.name
        town_county["nation"] = town.county.nation.name
        return town_county
    except MultipleResultsFound:
        return "bad Gateway!", 502
    except NoResultFound:
        return "Results not found!", 404

I'm not really sure if i have to change my query. Kindly advice.

David
  • 51
  • 1
  • 1
  • 8
  • 2
    you should add the content-type in the response headers, i don't know python so i can not tell you how to do that – Lk77 Apr 27 '22 at 08:48
  • @Lk77 which response header? can you edit it for me? thank you. Because i know it wrong to put headers in the url for get request. T hank you – David Apr 27 '22 at 08:54
  • Take a look [here](https://github.com/axios/axios/issues/86#issuecomment-139638284) and [here](https://stackoverflow.com/a/48754473/17089665), they might be of help. It seems you have to have data filled with something in order for the header to not be automatically discarded. – S. Dre Apr 27 '22 at 09:13
  • Just in case someone is using a `RequestParser` from Python `flask-restx` dependency, set the `location` parameter in the `add_argument` function. If not, it'll raise that error – cpinamtz Oct 19 '22 at 11:04

2 Answers2

3

As commented in this GitHub issue:

https://github.com/axios/axios/issues/86#issuecomment-136505548

https://github.com/axios/axios/issues/86#issuecomment-139638284

Your GET request must have some data passed along it. data: {} should do the trick.

Some people find it kind of misleading. The explanation for this behaviour is here:

Content-Type describes what format the request data is in. If there is no request data, there is no need to specify the Content-Type.

S. Dre
  • 647
  • 1
  • 4
  • 18
  • i think the correct header is `Accept: application/json` not `Content-Type: application/json` – Lk77 Apr 27 '22 at 09:36
  • It might be, although [it seems this 'issue' also affects the `Accept` header](https://github.com/axios/axios/issues/86#issuecomment-405930811) – S. Dre Apr 27 '22 at 09:39
  • thank you @S.Dre i will take a look at them – David Apr 27 '22 at 09:52
  • Please let us know if it solved your issue! – S. Dre Apr 27 '22 at 11:55
  • 1
    @S.Dre i did manage, i had to modify the backend, i was validating data in the backend, i had to get ```get``` method out of validation. thank you. – David May 04 '22 at 06:27
0

For my application this was a breaking change from werkzeug v2.3.0

for accepting without application/json header I use flask and werkzeug v2.0.3

DevBush
  • 383
  • 1
  • 3
  • 10