Home > Mobile >  HTTP 400 response received
HTTP 400 response received

Time:03-09

The following HTTP request was sent and an HTTP 400 response was received. What is wrong with the request that caused it to fail?

 POST /api/v2/organizations/4523jddfgd/projects HTTP/1.1
 Host: www.reddit.com
 Content-Type: application/json
 Authorization: Bearer VALID_API_TOKEN

   [
     "name": "Project 1",
     "package": "Standard",
     "address": {
     "street": "12456 Main St",
     "city": "Dallas",
     "zip": "75200",
     "state": "TX",
     "country": "USA"
     }
  ]

This is an interview question, can you help guys ?

CodePudding user response:

Have a look at the MDN wiki page about HTML status code 400.

Status code 400 points to a client side error e.g. malformed request body. Another reason could be that a required header is missing. As indicated in the MDN wiki page you should not attempt to send the very same request unmodified again as the result will be the same as it is not a server side issue.

What you should do instead is: consult the API documentation again and make sure that no header is missing and the request body is valid (i.e. valid JSON/ XML/ whatever in the correct structure).

Looking at your request I can see that the body is not valid JSON.

[
     "name": "Project 1",
     "package": "Standard",
     "address": {
     "street": "12456 Main St",
     "city": "Dallas",
     "zip": "75200",
     "state": "TX",
     "country": "USA"
     }
  ]

This cannot be parsed, hence the error. An array cannot hold properties. What is missing is a { and } after the [ and before the closing ]. Using the following request body should work.

[
   {
      "name":"Project 1",
      "package":"Standard",
      "address":{
         "street":"12456 Main St",
         "city":"Dallas",
         "zip":"75200",
         "state":"TX",
         "country":"USA"
      }
   }
]

EDIT

As @Evert pointed out, it is more likely that you need to replace [] with {} as POST requests are used to create single new resources. But the only way you can be absolutely sure is by reading the API documentation.

{
   "name":"Project 1",
   "package":"Standard",
   "address":{
      "street":"12456 Main St",
      "city":"Dallas",
      "zip":"75200",
      "state":"TX",
      "country":"USA"
   }
}
  • Related