2

Using Directus online (https://projectcode.directus.app install of local or server install). I can successfully create item but data I send is always null.

This is happening running my code

axios.post( 
    `${endpoint}/items/collection_name`, 
    { data: { "field_name": "testthis" } }, 
    { headers: headers } 
  );

And using Postman. Result is always

date_created: "2022-05-05T13:55:47"
id: 14
field_name: null

I can get items, search and filter with no issue. What am I missing?

ETA: Postman works with graphql and query

mutation {
    create_collection_name_item(data: { field_name: "Hello again!" }) {
        field_name
    }
}

So why is api getting null values?

ETA 2: Postman api headers

Request

Authorization: Bearer the_very_long_token
User-Agent: PostmanRuntime/7.29.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 45943ddb-5c60-4d45-8887-a05207f9469e
Host: k2g2xa7b.directus.app
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 13

Response

Content-Type: application/json; charset=utf-8
Content-Length: 135
Connection: keep-alive
Date: Mon, 09 May 2022 14:04:54 GMT
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: Content-Range
Cache-Control: no-cache
Content-Security-Policy: script-src 'self' 'unsafe-eval';worker-src 'self' blob:;child-src 'self' blob:;img-src 'self' data: blob: https://cdn.directus.io;media-src 'self' https://cdn.directus.io;connect-src 'self' https://*;default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;frame-ancestors 'self';object-src 'none';script-src-attr 'none';style-src 'self' https: 'unsafe-inline'
Etag: W/"87-n/ABsgX1O7F1qQ7x0xdJgMV7VDc"
Server: Caddy
Vary: Origin, Cache-Control
X-Powered-By: Directus
X-Cache: Miss from cloudfront
Via: 1.1 dcd16c430149132ea12a5783d54ff114.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: YTO50-P2
X-Amz-Cf-Id: Z19onMWqxvINuZ7iMQIJQeSRFrgW12a4gxZtcWPyubaWQCeaUnfLfA==

2 Answers 2

3

What headers do you provide? It seems that Content-Type is not determined automatically, so it may be as simple as including Content-Type: application/json in your headers.

Sign up to request clarification or add additional context in comments.

3 Comments

Will add Postman request and response headers to original post.
Hurray! If I change from using Postman Body > x-www-form-urlencoded to Body > raw and add Content-Type: application/json to header it sends data. Thanks!
Looks like for some reason it defaulted to Content-Type: application/x-www-form-urlencoded; glad it works!
0

in python:

import requests, json

token = 'your token'

headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.5',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
    'Content-Type': 'application/json',
    'Authorization': f"Bearer {token}",
}

payload = [
    {
        "title" : "my first item",
    }
]
r= requests.post(
    url = "https://cms.uat.<your subdomain>.com/items/<your collection>",
    data = json.dumps(payload),
    headers = headers
)

print(r.json())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.