Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't create items using the REST API #22451

Closed
maxacarvalho opened this issue May 9, 2024 · 1 comment
Closed

I can't create items using the REST API #22451

maxacarvalho opened this issue May 9, 2024 · 1 comment

Comments

@maxacarvalho
Copy link

Describe the Bug

Hi,

I'm trying out Directus using Docker to see if it will be a good fit for my needs.
I have a very basic project running locally.

I created one collection called payloads. The collection has one field, input, and it's set as mandatory.

Now, I'm trying to use the REST API to create an item. It won't work, it gives me an error saying the required field input is not present.
If I change the field to be optional I can create the item but the payload is not recognised.

Here's the cURL request (I'm using Postman):

curl --location 'http://localhost:8055/items/payloads' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer rxRy-AS39AfvUd-1OWgHT7ZQ5OlFS5B-' \
--data '{
    "input": {
        "test": "test"
    }
}'

And the response:

{
    "errors": [
        {
            "message": "Validation failed for field \"input\". Value is required.",
            "extensions": {
                "code": "FAILED_VALIDATION",
                "field": "input",
                "type": "required"
            }
        }
    ]
}

To Reproduce

Start a new self-hosted project

Here's my docker-compose schema:

services:
  app:
    image: directus/directus:10
    ports:
      - 8055:8055
    volumes:
      - ./database:/directus/database
      - ./uploads:/directus/uploads
      - ./extensions:/directus/extensions
    depends_on:
      - mysql
    environment:
      KEY: "integra-hub"
      SECRET: "integra-hub"
      ADMIN_EMAIL: "integra-hub@integra-hub.com"
      ADMIN_PASSWORD: "secret"
      DB_CLIENT: "mysql"
      DB_HOST: "mysql"
      DB_PORT: "3306"
      DB_DATABASE: "integra-hub"
      DB_USER: "integra-hub"
      DB_PASSWORD: "secret"
      WEBSOCKETS_ENABLED: "true"
    networks:
      - integra-hub

  mysql:
    image: mysql:8
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: 'secret'
      MYSQL_DATABASE: 'integra-hub'
      MYSQL_USER: 'integra-hub'
      MYSQL_PASSWORD: 'secret'
    volumes:
      - database:/var/lib/mysql
    networks:
      - integra-hub

networks:
  integra-hub:
    driver: bridge

volumes:
  database:
    driver: local

Create a collection

  • Name: payloads
  • Fields:
    • id
    • date_created
    • date_updated
    • input (JSON | required)

Use Postman to send a request

curl --location 'http://localhost:8055/items/payloads'
--header 'Content-Type: application/json'
--header 'Authorization: Bearer rxRy-AS39AfvUd-1OWgHT7ZQ5OlFS5B-'
--data '{
"input": {
"test": "test"
}
}'

Directus Version

v10.11.00

Hosting Strategy

Self-Hosted (Docker Image)

@hanneskuettner
Copy link
Contributor

Are you sure that you are actually sending the 'Content-Type: application/json' header? What you are describing is consistent with accidentally sending form encoded data which is not recognized by the body parser. Like, did you actually run the curl request you have provided?

Using the request you have provided I am able to successfully create items in the payloads collection.

I am going to close this issue at it seems to not be a problem with Directus itself, but I'm happy to reopen if there is new information!

@hanneskuettner hanneskuettner closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

No branches or pull requests

2 participants