Why i have the bad request 400?

mezigar

I'm trying to write test for my simple CRUD. But i have a problem with testing post request: There is my test:

    def test_post_product(self):
        product1 = Product.objects.create(title='Chicken Breast',proteins=24.00,carbs=0.00,fats=3.00,calories=113)
        serialized_data = ProductSerializer(product1).data 
        response = self.client.post(reverse('product-list'), data=serialized_data, content_type='application/json')
        print(response)
        self.assertEqual(HTTP_201_CREATED, response.status_code)

But in self.assertEqual i have response status code 400 instead of 201. What i'm doing wrong?

chivalrous-nerd

It looks like you are creating a product in your database and then trying to post the serialized data for that product to the product-list endpoint. However, since the product already exists in the database, this will result in a 400 response because you are trying to create a duplicate entry.

Instead of creating the product in the database and then trying to post the serialized data, you can create the data for the product directly and then post that to the endpoint. Here is an example of how you could do that:

def test_post_product(self):
    # Create the data for the product you want to post
    data = {
        'title': 'Chicken Breast',
        'proteins': 24.00,
        'carbs': 0.00,
        'fats': 3.00,
        'calories': 113
    }

    # Post the data to the endpoint
    response = self.client.post(reverse('product-list'), data=data, content_type='application/json')

    # Assert that the response has a 201 status code
    self.assertEqual(HTTP_201_CREATED, response.status_code)

Since the product does not already exist in the database, the endpoint should return a 201 response.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am i getting Http/1.1 400 Bad request?

Why am I getting 400 bad request with AngularJs post?

Why do I get a bad request (code 400) with HttpWebReponse?

Why do I see a "400 (Bad Request)" in my developer console when sign-in to Firebase Authentication fails?

Postman - 400 Bad Request

Why do I get BioPython HTTPError: HTTP Error 400: Bad Request when I use Esearch and Efetch

Why am I getting the 400 error (Bad request)

Why i`m getting 400 Bad Request in Html Drop down list

Why does this Python POST Request returns 400 Bad Request Code?

AJAX: 400 bad request

Why I get a "Bad Request" Error 400 when I try to connect with Api with fetch?

$.ajax 400 Bad Request

Why Share on LinkedIn API return 400 Bad Request?

Angularjs Bad Request (400)

Node, express, Monk, mongodb: 400 bad request error on post request. Why am I getting this error?

Rails, I keep having 400 Bad Request on create and update and can't find out why

Guzzle 400 Bad Request

400 bad request with Ajax

bad request error 400

I dont know why I get a 400 bad request error for sending a discord webhook. Everything seems fine but I am new at this

why am i getting a 400 (bad request) error when using ajax?

Clarifying "400 - Bad Request"

Why am I getting a '400: Bad Request' when i try to change my Discord avatar?

Why am I getting a "400: Bad Request" but the request successfully posts to my DB

Django: Why am I getting a 400 bad request error?

Why do I get a 400 Bad Request error from api

Spring bad Request 400

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: I am not able to figure why I am getting this error

I have an error during the session, code 400, message Bad request version ('׫')

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive